开发者

Silverlight resource constructor always return to internal

开发者 https://www.devze.com 2023-01-20 16:43 出处:网络
When I modify my resource 开发者_JAVA技巧file (.resx) add text or modify, the constructor of my resource always go to internal and after that, when I run my silverlight I have an error in my binding X

When I modify my resource 开发者_JAVA技巧file (.resx) add text or modify, the constructor of my resource always go to internal and after that, when I run my silverlight I have an error in my binding XAML.

Is there a way to avoid this scenario? I need to go in the designer of my resource and put the constructor to public to solve the problem

I use my resource like this in my xaml file

 <UserControl.Resources>
        <resources:LibraryItemDetailsView x:Key="LibraryItemDetailsViewResources"></resources:LibraryItemDetailsView>
    </UserControl.Resources>


<TextBlock FontSize="12" FontWeight="Bold" Text="{Binding Path=FileSelectedText3, Source={StaticResource LibraryItemDetailsViewResources}}"></TextBlock>


Another way to do this without code changes is as below. Worked well for me.

http://guysmithferrier.com/post/2010/09/PublicResourceCodeGenerator-now-works-with-Visual-Studio-2010.aspx


You can create a public class that exposes the resources through a property:

public class StringsWrapper
{
    private static LibraryItemDetailsView _view = null;

    public LibraryItemDetailsView View
    {
        get
        {
            if (_view == null)
            {
                _view = new LibraryItemDetailsView();
            }
            return _view;
        }
    }
}

Then in your XAML you can access your resource:

<UserControl.Resources>
    <StringsWrapper x:Key="LibraryItemDetailsViewResources"></StringsWrapper>
</UserControl.Resources>


<TextBlock FontSize="12" FontWeight="Bold" Text="{Binding Path=View.FileSelectedText3, Source={StaticResource LibraryItemDetailsViewResources}}"></TextBlock>

This way the resx constructor can be internal!


Well, what I did was to add a command line utility to pre-build event of each Silverlight project that replaces each internal string with public :)

You can edit pre-build and post-build events by: Right-clicking on a project -> Properties -> Build Events.

I used a utility called RXFIND, it's free and can replace a string within selected files using a RegEx regular expression.

Here's the command line I'm using:

"$(SolutionDir)ThirdParty\rxfind\rxfind.exe" "$(ProjectDir)Resources\*.Designer.cs" "/P:internal " "/R:public " /Q /B:2

Please note, that all my resources are located under Resource directory within each project and the command line utility resides in \ThirdParty\rxfind directory


I also have the same error. To solve the problem I just created a public class that inherits from the class representing the resources file, knowing that it must also be public class this is my exep:

public class TrackResourceWrapper : TrackResource
{
}

with: TrackResourceWrapper is inheriting class TrackResource is the class which is located in the code resource file behind (public class)


Simply:

  1. Add a new class that inherits from the resource class
  2. In the App.xaml file, modify the resource class which you created

Done!


The reason for this is that you shouldn't be instantiating the class yourself. You should instead always use ConsoleApplication1.Resource1.ResourceManager which itself instantiates the class for you.

Here, ConsoleApplication1 is the name of your assembly and Resource1 the name of your resource file.


I created a macro to do this for me for each file I edit. I still have to remember to run it, but it's a lot quicker. Please see my post.

0

精彩评论

暂无评论...
验证码 换一张
取 消