Silverlight 4 Equivalent to WPF “x:static”

泪湿孤枕 提交于 2019-11-27 05:42:43

问题


I'm working on a project that is based on an old project someone started and didn't finish. I was trying to use as much of their code as I could, so in doing so I ran into some tweaking issues.

Namely, when I put some of the old xaml in the new project there were some errors that were thrown regarding the "x:static" property and "Dynamic property."

here are the error messages themselves:

Error 1: The type 'DynamicResource' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

Error 2: The type 'x:Static' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

Some notable points that I think is causing the errors: the old project was programmed in VS2008, WPF, v3.5 .Net framework; whereas I am programming in VS2010, Silverlight 4, .Net framework v4.0.

I realize there are differences from WPF to Silverlight as far as xaml goes and there are plenty of differences from the different .Net framework versions and editions of Visual Studio. But I just can't seem to find a fix for this anywhere so I didn't know if there was just a library I was missing or just something I'm simply overlooking or what.

I can recreate this if need be, but like I said, I'd rather use as much of the old code as I can as long as the tweaking doesn't cause more trouble than what it's worth.


回答1:


Unfortunately, you can't directly use the DynamicResource and Static keywords in a Silverlight's subset of XAML, but you can mimic their behavior. Here is the article on the topic:

  • {x:Type} and {x:Static} in Silverlight

In general, there is no easy way to migrate a project from WPF to Silverlight. They have very much in common, but strictly speaking are a different technologies.




回答2:


Another way to achieve binding to static properties - to bind in code. Below is an example.

Main application class:

public partial class App : Application
{
    public static MyViewModel MyViewModel { get; private set; }

    // ...
}

Main window markup:

<TextBlock Loaded="MyTextBlockLoaded" />

Main window back-code:

public partial class MainPage : PhoneApplicationPage
{
    // ...

    private void MyTextBlockLoaded(object sender, RoutedEventArgs e)
    {
        TextBlock textBlock = ((TextBlock)sender);
        if (textBlock.Tag == null)
        {
            textBlock.Tag = true;
            Binding bind = new Binding("MyInfo");
            bind.Source = App.MyViewModel;
            bind.Mode = BindingMode.OneWay;
            textBlock.SetBinding(TextBlock.TextProperty, bind);
        }
    }
}

Maybe the TextBlock.Tag approach of checking, was Binding already set or not, isn't the most elegant one, but it works.



来源:https://stackoverflow.com/questions/3373926/silverlight-4-equivalent-to-wpf-xstatic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!