Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 4 Equivalent to WPF "x:static"

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.

like image 997
AmbiguousX Avatar asked Jul 30 '10 17:07

AmbiguousX


2 Answers

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.

like image 96
n535 Avatar answered Nov 05 '22 00:11

n535


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.

like image 29
Alec Mev Avatar answered Nov 04 '22 23:11

Alec Mev