Ric*_*ich 13 .net c# silverlight wpf xaml
我无法在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}" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
不幸的是,Silverlight在功能方面有很多限制,你刚刚找到其中一个.SL2不支持StaticMarkupExpression.您也无法自己定义它.
例如来自ms的家伙:http://blogs.msdn.com/edmaia/archive/2008/11/23/animating-objects-visibility-in-silverlight.aspx
诀窍可能是使用像这样的对象
class Helper{
public string Value {get{return Page.Test;}}
// implement INotifyPropertyChange if you want updates
}
Run Code Online (Sandbox Code Playgroud)
然后
<Grid.Resources>
<somexmlns:Helper x:Key="Helper"/>
</Grid.Resources>
<TextBlock Text="{Binding Value, Source={StaticResource Helper}}"/>
Run Code Online (Sandbox Code Playgroud)
不幸的是,看起来Silverlight不支持绑定到静态属性:什么是{x:Static sdfsdf}等价物?
归档时间: |
|
查看次数: |
23237 次 |
最近记录: |