2017-06-05 118 views
-1

我有一些奇怪的錯誤,我試圖擺脫困境。我爲我的一個應用程序創建了一個自定義的.Cursor。我已經將自定義光標加載到圖像文件夾中的項目中,將其設置爲我的資源,並創建了一個靜態類來打開媒體流,並將光標傳遞到窗口。從那裏,在我的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的命名空間中的名稱: POS系統「

然而,這個錯誤是不正確的,但它導致我無法在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; 
    } 
    } 
} 
+0

也許'POProperties'在不同的命名空間(不是'POSystem')或不是一個類。你應該提供一些額外的細節 –

+0

我給這個問題增加了一些代碼。 – ARidder101

回答

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)); } 
    } 
} 

POProperties代碼:

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)); } 
} 

這個方法我不再獲得從綁定任何錯誤它運作良好。我知道這個方法prier的問題,但我認爲靜態類方法會更好。顯然我錯了。