C# 使用在 Silverlight 中的代码中创建的 XAML 中的静态对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/640154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:23:11  来源:igfitidea点击:

Using static objects in XAML that were created in code in Silverlight

c#.netwpfsilverlightxaml

提问by Rich

I couldn't get this to work in Silverlight, so I created two test projects. One simple WPF project and one simple Silverlight project that both do only one thing: set a public static readonly variable in code, and use it in a completely bare bones XAML. In WPF, works without a hitch. In Silverlight, I get the following compiler warning and runtime error:

我无法在 Silverlight 中使用它,所以我创建了两个测试项目。一个简单的 WPF 项目和一个简单的 Silverlight 项目都只做一件事:在代码中设置一个公共静态只读变量,并在完全裸露的 XAML 中使用它。在 WPF 中,工作顺利。在 Silverlight 中,我收到以下编译器警告和运行时错误:

Warning 2 The tag 'Static' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml'...

警告 2 XML 命名空间“ http://schemas.microsoft.com/winfx/2006/xaml”中不存在标记“Static ”...

and

Invalid attribute value {x:Static SilverlightApplication3:Page.Test} for property Text. [Line: 7 Position: 25]

属性文本的无效属性值 {x:Static SilverlightApplication3:Page.Test}。[行:7 位置:25]

I'm assuming this is not supported in Silverlight 2, or am I just missing something really simple? Here's the full code for both just in case it's the latter:

我假设 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>

and here's the SL version:

这是 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>

采纳答案by Andy

Unfortunately, it looks like Silverlight doesn't support binding to static properties: What is the {x:Static sdfsdf} equivalent?

不幸的是,Silverlight 似乎不支持绑定到静态属性:{x:Static sdfsdf} 等效项是什么?

回答by user76035

Unfortunately Silverlight has many limits with respect to functionality and you just found one of them. StaticMarkupExpression is not supported by SL2. You also can't define it by yourself.

不幸的是,Silverlight 在功能方面有很多限制,而您只是发现了其中之一。SL2 不支持 StaticMarkupExpression。你也不能自己定义它。

e.g. guy from ms: http://blogs.msdn.com/edmaia/archive/2008/11/23/animating-objects-visibility-in-silverlight.aspx

例如来自女士的家伙:http: //blogs.msdn.com/edmaia/archive/2008/11/23/animating-objects-visibility-in-silverlight.aspx

The trick may be to use an object like

诀窍可能是使用像

class Helper{
    public string Value {get{return Page.Test;}} 

// implement INotifyPropertyChange if you want updates
}

And then

进而

<Grid.Resources>
     <somexmlns:Helper x:Key="Helper"/>
</Grid.Resources>

<TextBlock Text="{Binding Value, Source={StaticResource Helper}}"/>

回答by user76035

You can actually bind to static properties as long as the class is not a static class. So using the previous example of the Helper class:

只要类不是静态类,您实际上就可以绑定到静态属性。所以使用之前的 Helper 类示例:

public class Helper
{
    public static string Value{ get {return Page.Test;} }
}

The XAML will stay the same.

XAML 将保持不变。

回答by user76035

I just noticed that you had a secondary question about binding to a color. I don't think it can be done in Silverlight. I'm pretty sure the minimum requirement for a binding target in Silverlight is FrameworkElement.

我刚刚注意到你有一个关于绑定到颜色的次要问题。我不认为它可以在 Silverlight 中完成。我很确定 Silverlight 中绑定目标的最低要求是 FrameworkElement。

回答by copelia

A static object will be instantiated only once and will persist until the end of the program.A static object can retain its state even when it is not in scope, but only visible within their local scope.

静态对象只会被实例化一次,并且会一直持续到程序结束。静态对象即使不在范围内也可以保持其状态,但只能在其本地范围内可见。