Reputation: 4455
The idea is very straightforward, throughout uwp applications multiple controls will have their x:uid attribute defined on them and based on that we will have .resw localized file with default string values I know how that works.
However when the application loads we get a custom json or xml string from server side through an api and it will be like the "resw" string where it will have collection of keys and values and it is supposed to override the currently being used "resw" values, bcz the keys will be same in resw file and the json string which we get from server but the values might be different, so can we override those resw string values at runtime somehow?
if that isnt possible can we get a List of all x:UIDs on a given page when the page loads? and then loop through them and update their properties based on the values we have coming from our server side json?
<AppBarButton
x:Name="appbar2"
x:Uid="appbar2"
Width="200"
Height="56"
Icon="Accept"
Style="{ThemeResource AppBarButtonNavigationStyle}" />
I am aware on any given control we have the .Name property but we dnt have .uid property which is why I was wondering if there is a workaround way to get it?
Ive looked into official documentation but apparently we cannot set values on "resw" resources they r readonly, which is a bummer, in our case we will only need english language so we always will have 1 resw file.
The aim is that all controls can have all of their default property values already set on xaml or with x:uid but when we get our values from server side only those controls with only their properties which exist in the "server side json" should be updated.
Ive tried to create a class with all dynamic properties I need and each of these properties have a default value to it, and then I bind it to UI with x:Bind, and when we get json from server side I just convert it to that class so all its values get updated and it works as expected only limitation here is everytime I have to add a new control or a new property of the same control I have to add it to json as well as client side xaml code and that model class as well, which is not ideal because we want to only update or add new properties from server side without updating the client side.
for the code give above, we have a control with x:UID=appbar2 now we only have to assign this once on xaml, then in resw resource file it will have values like "appbar2.Label=Label1" this will be its default value, but then server side json comes in and we have key, value pair as "appbar2.Label-Label2" this will update its label to "label2" and if the json also has "appbar2.Header=h1" it should also update header of appbar2 button, assuming this control had a property "header" on it.
Thanks.
Upvotes: 0
Views: 112
Reputation: 1580
Some other resources about alternatives to doing resource loading.
See this blog here for using a Markup Extension instead. And beyond that there's the ReswPlus project.
I don't know if either will do what you want. Why is all your localization coming from a server-side resource instead of shipping with the app itself? Dynamically updating things sounds like you're trying to exploit the x:uid system as it wasn't designed for this scenario.
If you really need to do it this way, I'd suggest instead just creating a static class
which exposes dependency properties (or uses INotifyPropertyChanged) and use {x:Bind local:MyStaticClass.MyPropertyNameForThisLabel, Mode=OneWay}
(or set OneWay
for all above for the page using x:DefaultBindingMode
.
Then when your server resource is downloaded, it can update all the properties on this static class and all the bound properties on it will be automatically updated through their bindings like anything else would be.
Upvotes: 1
Reputation: 169160
If you retrieve the resources at runtime, it doesn't make much sense to use the x:Uid
directive to localize your app. You'd better come up with something else to "mark" the elements and localizable, for example using an attached property, and then set the properties once you have received the resources.
I am aware on any given control we have the .Name property but we dnt have .uid property which is why I was wondering if there is a workaround way to get it?
I am afraid not:
Set value of x:Uid programmatically
Upvotes: 2