How to exclude InfoBarMessageControl in Orchestra Shell MahApps? - mahapps.metro

I'm building an application using the Orchestra MahApps shell (built on Catel), but would like to exclude the InfoBarMessageControl. It seems there is an option in the Catel DataWindow constructor to set the generation field, but it doesn't appear this option is exposed in the ShellWindow constructor. What would be the proper method for disabling (InfoBarMessageControlGenerationMode.None) while using the MahApps shell?
Also listed on https://github.com/WildGums/Orchestra/discussions/542, but I'm not sure if anyone checks there.

Good question, I think it's not possible out of the box. For now a workaround would be to use private reflection to set this field using this pseudo code:
var field = GetType().GetFieldEx("_infoBarMessageControlGenerationMode");
field.SetValue(this, InfoBarMessageControlGenerationMode.None);
Make sure to call this code before OnInitialized is called the WPF.

Related

How to get the old value of a TrackBar without a custom control or separate field?

I am trying to get the old Value of a TrackBar when the Scroll event gets raised. I could do this by creating a separate field and storing that value whenever it changes, or overriding the event in a custom control however, I am already using the built-in TrackBar and I'd prefer to not have to redesign my forms.
The Microsoft documentation does not seem to have any information on this and the EventArgs parameter on the OnScroll and OnValueChanged methods seem pretty generic.
Is there a way to achieve this and how?
Sources:
Microsoft Documentation: TrackBar.OnScroll Method
Microsoft Documentation: TrackBar.Scroll Event
How to override method and event in WinForm UserControl in C#?
Microsoft Documentation: TrackBar.ValueChanged Event
Microsoft Documentation: TrackBar.Value Property
Microsoft Documentation: TrackBar.OnValueChanged Method
I believe there isn't a built-in way to achieve what you're looking for.
To address your concern about having to replace all the TrackBar controls on a form with the derived custom control, here's how I usually do it:
Create your control class which inherits the TrackBar control.
Open your FormName.Designer.cs or FormName.Designer.vb file.
Click Ctrl+H to open the "Find and replace" window.
Enter System.Windows.Forms.TrackBar in the "Find" field, and YourNameSpace.CustomTrackBar in the "Replace with" field.
Click "Replace all" (You can do it one by one if you're afraid to mess something up).
Rebuild your project.
Hope that helps.

Using Graphics.TextRenderingHint with TextRenderer.DrawText to get anti-aliased text in ToolStripItem

I need to enhance one legacy WinForms app to support the inheritance of the TextRenderingHint setting in the app forms.
For simplicity, let's suppose that we have a main form with the TextRenderingHint property of the System.Drawing.Text.TextRenderingHint type. This property specifies the text quality in the main form and must be inherited in dialog forms called from this main form. The text in the main form is drawn using Graphics.DrawString method, and there is no problem to support various settings of the main form's TextRenderingHint property (in fact, it is simply assigned to Graphics.TextRenderingHint before drawing text with Graphics.DrawString).
The problem is that the interface of one of the dialogs is based on the WinForms ToolStrip component and I need to redefine text drawing in its items to support the TextRenderingHint setting of the main form.
After searching the Internet and analyzing the source code of the ToolStripRenderer class in a reflector app, I came to the conclusion that the best way to implement what I need is to use a custom ToolStrip renderer with the redefined OnRenderItemText method.
I found the following code in the default implementation of the OnRenderItemText method in the ToolStripRenderer class (see the full code at the bottom of my question):
graphics2.TextRenderingHint = TextRenderingHint.AntiAlias
TextRenderer.DrawText(graphics2, text, textFont, New Rectangle(Point.Empty, size), color, textFormat)
, which gave me an idea that I could try to solve my problem with this simple implementation of OnRenderItemText in a class derived from ToolStripRenderer:
Protected Overrides Sub OnRenderItemText(e As ToolStripItemTextRenderEventArgs)
e.Graphics.TextRenderingHint = _MainFormTextRenderingHint
MyBase.OnRenderItemText(e)
End Sub
My idea is based on the fact that if we do not use vertical text in ToolStrip items (we don't), the basic OnRenderItemText method "just" calls TextRenderer.DrawText and theoretically my idea could work. Unfortunately, in practice this works only for the TextRenderingHint.ClearTypeGridFit option - at least, on my dev pc.
The only viable idea I see now is complete rewriting of OnRenderItemText and using Graphics.DrawString inside. Am I right? Are there other solutions to my problem?
UPDATE #1. I encountered a problem trying to reimplement OnRenderItemText based on Graphics.DrawString. It seems, it's impossible to convert text format flags passed to TextRenderer.DrawText (the TextFormatFlags enumeration) to the StringFormatClass parameter Graphics.DrawString expects...
UPDATE #2. The default implementation of the OnRenderItemText method in the ToolStripRenderer class is the following:

WPF/MVVM - how to execute commands on start-up?

I'm writing a WPF app following MVVM principles.
I wan't to execute a command on startup - I'm in doubt as to what is the best method?
The ViewModel should be indifferent as to whether there are any views, right?
Is it then 'ok' to do stuff as the last thing in VM constructor? Or is there an event for 'application is now up and running, all initialization has completed' that I can hook onto?
It feels wrong to wire it into the view model?
To be a bit concrete, I'd like to do as Visual Studio and load 'most recent solution' if the user has selected that in preferences. If the user manually loads a solution through GUI I start the flow in a MainFormViewModel and I could handle the load in last lines of constructor there?
Any thoughts?
Anders, Denmark
The Windows.Interactivity approach or asynchronously loading their preferences from the constructor are equally viable - an alternative exists if your using the MefedMVVM framework.
You can also import the IContainerStatus to attach to the view being loaded entirely from the view model (and therefore nothing to forget doing in the XAML) More info is available here
I really wanted to avoid calling methods from within the ViewModel constructor - and in my view activating events from constructor is doing just that (or at least when using Prism as I am (should have mentioned that).
I ended up doing the simple thing and simply calling a Loaded method on my main ViewModel after construction has ended.
Nonetheless, Scott singled out as the answerer - thank you all for taking the time. I appreciate your point of view even if I chose another way in the end.
Anders, Denmark
var mainViewProvider = ObjectFactory.GetInstance<IMainViewProvider>();
var mainWindowViewModelProvider = ObjectFactory.GetInstance<IMainWindowViewModelProvider>();
var mainWindow = mainViewProvider.GetView();
var mainWindowViewModel = mainWindowViewModelProvider.GetViewModel();
mainWindow.DataContext = mainWindowViewModel;
mainWindowViewModel.Loaded(Settings.Default.LoadLatestOnStart);
mainWindow.Show();
What you can do is use your MainForm's (the one which needs to load the solution) Loaded event.
Use Windows.Interactivity EventTrigger to attach a command to Loaded event. And on that command's execution, load the solution.
I would introduce Controllers which are responsible for the UI workflow. They know when the application has started and they can execute Commands. This doesn't violate with the MVVM pattern. If you are interested how this work then you might find the sample applications of the WPF Application Framework (WAF) interesting.
That's ok if and when you setup the context in code, but not if you do it from xaml - for the sake of transparency and flexibility one should consider supporting both code and xaml.
Another approach could be to trigger something on your model from xaml when certain criteria has been met.
I believe a storyboard could be used for this approach.
/Torben Falck, Strongminds, Denmark, www.strongminds.dk

Calling a WPF Application and modify exposed properties?

I have a WPF Keyboard Application, it is developed in such a way that an application could call it and modify its properties to adapt the Keyboard to do what it needs to. Right now I have a file *.Keys.Set which tells the application (on open) to style itself according to that new style. I know this file could be passed as a command line argument into the application.
That would not be a problem. My concern is, is there a way via a managed environment to change the properties of the executable as long as they are exposed properly, an example:
'Creates a new instance of the Keyboard Application
Dim e_key as new WpfApplication("C:\egt\components\keyboard.exe")
'Sets the style path
e_key.SetStylePath("c:\users\joe\apps\me\default.keys.set")
e_key.Refresh() 'Applies the style
e_key.HideMenu() 'Hides the menu
e_key.ShowDeck("PIN") 'Shows the custom "deck" of keyboard keys the developer
'Created in the style application.
''work with events and response
'Clear the instance from memory
e_key.close
e_key.dispose
e_key = nothing
This would allow my application to become easily accessible to other Touch Screen Application Developers, allowing them to use my keyboard and keep the functionality they need. It seems like it might be possible because (name of executable).application shows all the exposed functions, properties, and values. I just have never done this before. Any help would be appreciated, thank you in advance.
Thanks to Abe Heidebrecht , the best way is to create a class library and exposing the functions in a class.

How to use inheriting in WPF

I try to use inheriting in WPF. I have asked a question about this earlier, but no one answered correct. So I try to make a BaseWindow class with some UI elements and I want that other windows, that inherit my BaseWindow would have these UI elements. How to do that. My practise with WinForms applications dont work anymore. Maybe there are some simple examples or smth..? Thanks
If you want to inherit from a window, you need to create an implementation of a window in code only, and you can then inherit from this - note that your Window declaration in the XAML would need to change to point to this code, e.g.
<src:BaseWindow xmlns:src="clr-namespace:BaseWindowNamespace" ...>
If you define the base window using XAML, you'll get the following errors:
"'WpfApplication1.Window1.InitializeComponent()'
hides inherited member
BaseWindowNamespace.BaseWindow.InitializeComponent()'.
Use the new keyword if hiding was
intended"
"BaseWindowNamespace.BaseWindow cannot
be the root of a XAML file because it
was defined using XAML"
Now, at this point, I must point out that this is counter to the way that you should handle composition of your window. The "standard" way of doing this would be to use content templates to display user controls which could be swapped and styled to achieve different looks and functionality. In this way, the function of the window becomes that of a harness, and you can achieve a clean separation of content which can be easily implemented using MVVM.
Did you try the BasedOn tag? I don't know about windows, but that's what you use for UserControls AFAIK.

Resources