Localization in Silverlight 4 application using PRISM - silverlight

I am new to silverlight development and PRISM framework. We are using http://happynet.codeplex.com/ as a template. The localization is working well if resource text is bind directly in XAML. But it is not working well for :
Texts in ViewModel Classes.
Grid Header Column Texts in View's Code behind.
They requires reloading after language switch. How can it be done so that text change occurs on language switch?

I don't know much about Happynet as it is the first time I see the project.
It depends how they implement localization but if you want dynamic localization (eg. selecting a language in a combobox) it can be done. Follow the following steps to set it up:
http://vanderbiest.org/blog/2009/11/30/silverlight-resource-files-localization-language-specification/
To dynamically change it, you just need to set the current UI thread.
static void SetLanguage(string culture)
{
if (culture != System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.CultureName)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
}
}

Related

Refresh the UI of Windows Form

I'm working on an old project that supports Windows Forms.
This project contains some ResourceManager for the support of a few localizations. The idea is that you call ResourceManager["SomeResource"] instead of Resource.SomeResource and it returns you a localized string.
And these localized strings are used in the code of the initialization of the form. For example, you have Form1, and in Form1.Design.cs there is some code like this:
Label label1 = new Label();
label1.Text = ResourceManager["SomeResource"];
So the label will be created with an already localized string in the Text.
And we need to add the functionality of changing the UI language without reloading the Form.
We can just set the every Text property of every controls again. But it's a lot of code, the form contains a lot of controls.
We can call the Form.InitializeComponents(), this method will recreate all controls with new localized strings, but in some cases, it works slowly because it reloads some big data again.
Is there some other way to refresh all UI controls and get the new localized strings? Do Windows Forms support some mechanism like Binding in the WPF to create the "connection" between the Text properties and localized resources?
I think that you can achieve this by use of Invalidate() either on the form itself or on a container control that your other controls may be encompassed by.

File/Save for a VSIX custom file editor that hosts a WPF Control

I have been developing a Visual Studio extension to host custom diagrams within projects. The diagrams are developed using WPF user controls and I am now working on implementing the custom editor to host the diagrams. I followed the VSSDK example to implement a designer over an XML file (https://github.com/microsoft/VSSDK-Extensibility-Samples/tree/master/WPFDesigner_XML) and this all works to load the XML into the diagram and properly display the visuals. However, I also need to implement a custom save process with a custom file extension using File/Save and File/Save As. The WPF example has implemented saving the file in the background when idle. When I do a File/Save, the data is not properly saved to XML file through the custom save method and when I do a File/Save As, I am not seeing the custom file extensions.
I also reviewed the VSSDK example to implement a WinForms editor with a toolbox (https://github.com/microsoft/VSSDK-Extensibility-Samples/tree/master/Editor_With_Toolbox) which does demonstrate using File/Save, File/Save As and custom file extensions. It appears that the key to File/Save functionality is in implementing IPersistFileFormat and IVsPersistDocData. The methods for these interfaces also seem to require the Override implementation of WindowPane.Window.
The WinForm example for the Window Override simply returns the RichTextBox control as shown below:
public override IWin32Window Window
{
get
{
return editorControl;
}
}
However in using WPF, doing the same with the WPF user control does not work. I am looking for the bridge between these two examples to utilize IPersistFileFormat and IVsPersistDocData when hosting a WPF user control.
I did try to provide a window wrapper class that would utilize a Diagnostics Process window handle as discussed in this article: https://www.codeproject.com/articles/20347/how-to-get-iwin32window-handle-used-in-windows-for. But this returned a Default value and failed to work.
To summarize, how can I have the functionality of File/Save for a custom file editor that hosts a WPF control?
Not sure if this would help. I have a custom editor for FastReport Files (.frx) and show this in a WPF UserControl. In the CreateEditorInstance, I create a textbuffer and map the IPersistDocData interface
try
{
var localRegistry = ILocalRegistry)GetService(typeof(SLocalRegistry));
if (localRegistry != null)
{
IntPtr ptr;
var iid = typeof(IVsTextLines).GUID;
var CLSID_VsTextBuffer = typeof(VsTextBufferClass).GUID;
localRegistry.CreateInstance(CLSID_VsTextBuffer, null, ref iid, 1 /*CLSCTX_INPROC_SERVER*/, out ptr);
try
{
textBuffer = Marshal.GetObjectForIUnknown(ptr) as IVsTextLines;
}
finally
{
Marshal.Release(ptr);
}
// It is important to site the TextBuffer object
var objWSite = (IObjectWithSite)textBuffer;
if (objWSite != null)
{
var oleServiceProvider = (IOleServiceProvider)GetService(typeof(IOleServiceProvider));
objWSite.SetSite(oleServiceProvider);
}
persistDoc = textBuffer as IVsPersistDocData2;
}
Then, I use a ViewModel that implements IVsRunningDocTableEvents3 to catch the OnBeforeSave event in the RunningDocTable. When this fires, I write the contents of the editor to the textbuffer. Save and Save As can now do their thing with the correct file contents.
BTW: My main use for the IVsPersistDocData2 stuff is to manage the DocumentDirty flag when the custom editor changes (I don't save the contents of the editor to the underlying file until the OnBeforeSave event).

How do you bind a RichTextBox's Rtf property to a RTF resource at design time?

I have a RichTextBox that I wish to fill with RTF text at design time. This does not mean doing this:
richTextBox1.Rtf = #"<a bunch of rich text>";
which actually assigns the value at run time (or does it?).
I have created a project resource file called "TextResources.resx" with a resource named InstructionsRTF with a Value containing the rich text. How is this to be bound to the RichTextBox at design time?
Edited to add:
#hans-passant is correct, although the exact code I ended up using differs somewhat:
rtfInstructions.Rtf = TextResoures.InstructionsRTF;
where TextResources is the TextResources.resx in the project.
RichTextBox doesn't support binding. If it is already a precooked resource then trying to support this at design time doesn't make sense. It is just one line of code in the form constructor:
public Form1() {
InitializeComponent();
richTextBox1.Rtf = Properties.Resources.instructionsRTF;
}
If you want to get more adventurous at design time then that's possible too. You can create a UITypeEditor that lets you edit the RTF at design time. Code is here.

WPF : Definition Control Template by programmatically

We can make WPF Custom Control for overriding some control's look&feel, like icon button.
public class MyButton : Button {
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
}
...
}
But I think that way has a some problem. It causes a some issue when I distribute the Custom Cotrol 'MyButton'. Because MyButton dependent on external resource, the WPF Style MyButton. So I need to distribute two files : MyButton.cs and MyButton.WPF.
So, How can I definite a Conotrol Template by programmatically?
(Of cause, another way to solving the problem is making WPF User Control. but my point is not that.)
Note : I found some resources about this issue. That was a Inline XAML scripting. But to me, the XAML scripting is not option. Because I'm learning on WPF so I want to know WPF thatself, not a trick.
You do not distribute the code, you distribute a dll that contains the class and the generic.xaml
That way another designer/developer can 'override' the template and your template stays as a safe fall-back.
EDIT
Defining a Template in code is no fun (a lot of code, hard to debug, hard to maintain) but it can be done:
var template = new ControlTemplate(typeof(MyControl));
EDIT2
Another hack is to specify the template in a long string and use the XAML Parser to load it.

Creating Silverlight UserControl

I'm creating a silverlight user control that I should be able to drag and drop via blend. But this control needs to accept a map that is already on the page.
For eg.
Main.xaml contains a map control.
MapEditor.xaml contains buttons and other controls. In the .cs file, it needs to access a map control (the one in Main.xaml).
How do I go about getting this done?
I was thinking about adding a parameter in the contructor for MapEditor but how would I pass in the map as a parameter in design mode?
Thanks.
ps. I'm going to break out this control into a silverlight library so it could be used in multiple projects later.
You don't want to be giving your control a parameterised constructor, XAML will only construct types using their default constructor.
Simple Approach
The easiest approach would be to add DependencyProperty to your control to which you would assign the Map control (I'll use the type name MyMap in this example):-
public MyMap Map
{
get { return (MyMap)GetValue(MapProperty); }
set { SetValue(MapProperty, value); }
}
public static DependencyPropery MapProperty = new DependencyProperty("Map",
typeof(MyMap), typeof(MapEditor), new PropertyMetaData(null));
Now in Blend the Map property will appear in the Miscellaneous category in the Properties tab. You can then use the "Element Property" tab of the "Create Data Binding" to select the Map control to which it should bind.
Hard Core Approach
That said I would be inclined to build a proper customisable control following these guidelines Creating a New Control by Creating a ControlTemplate. With the addition that I would extend the ContentControl base class and include a ContentPresenter at the heart of the template. The control would make the assumption that the child control is a MyMap control.
This approach allows the entire appearance of the MapEditor control to be styled in Blend and it allows the Map control that is to be "edited" to be drap-drop onto the MapEditor as a child control.

Resources