Silverlight 4 Equivalent to WPF “x:static”

后端 未结 2 624
闹比i
闹比i 2020-12-08 20:47

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 int

相关标签:
2条回答
  • 2020-12-08 21:00

    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.

    0 讨论(0)
  • 2020-12-08 21:11

    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.

    0 讨论(0)
提交回复
热议问题