我无法在Silverlight中使用它,所以我创建了两个测试项目.一个简单的WPF项目和一个简单的Silverlight项目,它们只做一件事:在代码中设置一个公共静态只读变量,并在一个完全裸的XAML中使用它.在WPF中,工作顺利.在Silverlight中,我收到以下编译器警告和运行时错误:
警告2 XML名称空间" http://schemas.microsoft.com/winfx/2006/xaml " 中不存在"静态"标记...
和
属性Text的属性值{x:Static SilverlightApplication3:Page.Test}无效.[线路:7位置:25]
我假设Silverlight 2不支持这个,或者我只是错过了一些非常简单的东西?这是两者的完整代码,以防它是后者:
public partial class Window1 : Window
{
public static readonly string Test = "test";
public Window1()
{
InitializeComponent();
}
}
<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:WpfApplication4="clr-namespace:WpfApplication4">
<Grid>
<TextBlock Text="{x:Static WpfApplication4:Window1.Test}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是SL版本:
public partial class Page : UserControl
{
public static readonly string Test = "test";
public Page()
{
InitializeComponent();
}
}
<UserControl x:Class="SilverlightApplication3.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:SilverlightApplication3="clr-namespace:SilverlightApplication3"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{x:Static SilverlightApplication3:Page.Test}" …
Run Code Online (Sandbox Code Playgroud) 嗨所有的第一篇文章在这里:)让我们从我正在使用的代码片段开始:
public MyClass : INotifyPropertyChanged
{
private static MyClass _instance;
public static MyClass Instance
{
get
{
if (_instance == null)
_instance = new MyClass();
return _instance;
}
}
private bool _myProperty;
public bool MyProperty
{
get
{
return _myProperty;
}
set
{
if (_myProperty!= value)
{
_myProperty= value;
NotifyPropertyChanged("MyProperty");
}
}
}
private MyClass() { ... }
}
Run Code Online (Sandbox Code Playgroud)
如你所见,它是一个单身人士类.在我看来,我想在MyProperty上绑定一个控件.我最初的想法是在我的视图中使用以下内容将MyClass导入为静态资源:
<UserControl x:Class="Metrics.Silverlight.ChartView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:logic="clr-namespace:Metrics.Logic;assembly=Metrics.Logic">
<UserControl.Resources>
<logic:MyClass x:Key="myClass" />
</UserControl.Resources>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
然后像这样绑定它:
<Button Margin="5" Click="btnName_Click" Visibility="{Binding Source={StaticResource myClass}, Converter={StaticResource visibilityConverter}, …
Run Code Online (Sandbox Code Playgroud)