【问题标题】:Binding to static class/field绑定到静态类/字段
【发布时间】:2017-11-06 14:57:18
【问题描述】:

我有一个奇怪的错误,我正试图摆脱自己。我为我的一个应用程序创建了一个自定义 .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>

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

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

然而,这个错误是不正确的,但它导致我无法在 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;
    }
  }
} 

【问题讨论】:

  • 可能POProperties 位于不同的命名空间中(POSystem 除外)或者不是一个类。你应该提供一些额外的细节
  • 我在问题中添加了一些代码。

标签: c# wpf xaml data-binding static-class


【解决方案1】:

我对尝试使用静态属性方法感到不耐烦,所以我只是在现在非静态类中对 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)); }
}

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

【讨论】:

    猜你喜欢
    • 2014-09-10
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2013-08-17
    • 2014-01-29
    相关资源
    最近更新 更多