-1

我有一个奇怪的错误,我正试图摆脱自己。我为我的一个应用程序创建了一个自定义 .cur 光标。我已将自定义光标加载到图像文件夹中的项目中,将其设置为我的资源,并创建了一个静态类,用于为其打开媒体流并将光标传递给窗口。在我的 XAML 中,我有以下代码:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myCtrls="clr-namespace:UserControlsLibrary;assembly=UserControlsLibrary"
        xmlns:pos="clr-namespace:POSystem"
        x:Name="Main" x:Class="POSystem.MainWindow" 
        Title="Purchase Order"
        Cursor="{Binding Source={x:Static pos:POProperties.LTC_Cursor}, Mode=OneWay}"
        Height="850" Width="1100" Background="{StaticResource BackgroundImage}" 
        Icon="Images/logo_only_Xns_icon.ico">
    <Grid x:Name="TheGrid" Focusable="True" 
        MouseDown="ClearFocus_OnClick" Background="Transparent">
        <StackPanel x:Name="Panelicus">
        </StackPanel>
    </Grid>
</Window>

我在这里找到了这种绑定到静态类的方法,它在技术上是有效的。问题是,即使我已经构建了项目,甚至成功运行了代码,它也会显示一个无效的标记错误和描述:

名称空间“clr-namespace:POSystem”中不存在名称“POProperties”

但是,此错误不正确,但它导致我无法在 Visual Studio 中使用 XAML 设计器。

POProperties 代码:

namespace POSystem
{
  public static class POProperties
  {
    private static Cursor _ltc_cursor;
    public static Cursor LTC_Cursor
    {
      get => _ltc_cursor ?? new Cursor(new System.IO.MemoryStream(Properties.Resources.LTC_Custom_Cursor));
      set => _ltc_cursor = value;
    }
  }
} 
4

1 回答 1

0

我对尝试使用静态属性方法感到不耐烦,所以我只是在现在非静态类中对 POProperties 进行了静态实例化,实例化之外没有静态项。代码如下:

窗户:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myCtrls="clr-namespace:UserControlsLibrary;assembly=UserControlsLibrary"
        x:Name="Main" x:Class="POSystem.MainWindow" Title="Purchase Order" Cursor="{Binding LTC_Cursor, Mode=OneWay}"
        Height="850" Width="1100" Background="{StaticResource BackgroundImage}" Icon="Images/logo_only_Xns_icon.ico">
    <Grid x:Name="TheGrid" Focusable="True" MouseDown="ClearFocus_OnClick" Background="Transparent">
        <StackPanel x:Name="Panelicus">
            <TextBox x:Name="PONumLabel" Style="{StaticResource TBLabelStyle}" Width="250" Text="PO Number:"/>
            <myCtrls:DDTextBox x:Name="ItemsBox" Width="300" VerticalAlignment="Top" HorizontalAlignment="Left" MinHeight="25" Panel.ZIndex="1"/>
        </StackPanel>
    </Grid>
</Window>

窗口背后的代码:

namespace POSystem
{
  public partial class MainWindow : Window, INotifyPropertyChanged
  {
    private DataContext _data = new DataContext();
    internal DataContext Data { get => _data ?? new DataContext(); set => _data = value; }

    public MainWindow()
    {
      InitializeComponent();
      DataContext = POProperties.Instance;
    }    

    public void ClearFocus_OnClick(object sender, MouseButtonEventArgs e) { Keyboard.ClearFocus(); }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string Name = "")
    { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name)); }
  }
}

PO属性代码:

namespace POSystem
{
  public class POProperties : INotifyPropertyChanged
  {
    public POProperties() { }

    private static readonly POProperties instance = new POProperties();
    public static POProperties Instance { get => instance; }

    private UserInformation uinfo = new UserInformation();
    public UserInformation GetUserInformation { get => uinfo; }

    private Cursor _ltc_cursor;
    public Cursor LTC_Cursor
    {
      get => _ltc_cursor ?? new Cursor(new MemoryStream(Properties.Resources.LTC_Custom_Cursor));
      set => _ltc_cursor = value;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string Name = "")
    { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name)); }
}

这种方法我不再从绑定中得到任何错误,并且效果很好。我在提出问题之前就知道这种方法,但我认为静态类方法会更好。显然我错了。

于 2017-06-07T12:58:20.260 回答