How to re-style WPF application when using lower resolution screen - wpf

I am developng a WPF application which will be used on a couple of different PC types. The first type is more of a development machine with dual hi-res monitors; the second is a hand-held touch screen about 800x600.
Using the MVVM pattern, I have developed some different Views which display more or less information depending on the chosen mode. In general, the layout scales well enough.
I also have some dialog boxes but at the moment these are optimised for the hi-res mode. Unfortunately the buttons appear very small on the touch screen and are difficult to hit reliably.
I was wondering how to restyle the dialogs dynamically. I would like to have one application setting which sets the screen type and thereby control styles. So for example, if the screen is a touch screen then all buttons and menus will have a larger default size.
Are Themes the way to go? If so, can someone point me at a good tutorial? (not just using existing themes but also creating them)

In the interest of changing styles, I do the following:
Create a style sheet for each type of theme I want.
Set all style references throughout my project to DynamicResource instead of "StaticResource".
Use manipulation of ResourceDictionaries.
In this example, I have themes for High, Medium, and Low quality. My style sheets paths are:
/Assets/Styles/GlobalStylesLow.xaml
/Assets/Styles/GlobalStylesMed.xaml
/Assets/Styles/GlobalStylesHigh.xaml
The code for the manipulation works like this:
/// <summary>
/// Level of graphics quality enum.
/// </summary>
public enum GraphicsQuality
{
/// <summary>
/// Low
/// </summary>
Low = 0,
/// <summary>
/// Medium
/// </summary>
Medium = 1,
/// <summary>
/// High
/// </summary>
High = 2
}
/// <summary>
/// Sets the Application Resource Dictionaries based on selection.
/// </summary>
/// <param name="quality">The quality.</param>
/// <param name="onRedraw">The on redraw.</param>
public static void UpdateStyles(
Enums.GraphicsQuality quality = Enums.GraphicsQuality.High, Action onRedraw = null)
{
// Reset resource dictionaries
Application.Current.Resources.MergedDictionaries.Clear();
// Base style path
const string basePath = "/<project_base>;component/Assets/Styles";
// Evaluate global quality
switch (quality)
{
case Enums.GraphicsQuality.High:
LoadStyle(basePath + "/GlobalStylesHigh.xaml");
break;
case Enums.GraphicsQuality.Medium:
LoadStyle(basePath + "/GlobalStylesMed.xaml");
break;
case Enums.GraphicsQuality.Low:
LoadStyle(basePath + "/GlobalStylesLow.xaml");
break;
}
// Redraw
if (onRedraw != null)
{
onRedraw();
}
}
/// <summary>
/// Loads a specific style by Uri.
/// </summary>
/// <param name="stylePath">The style path.</param>
private static void LoadStyle(string stylePath)
{
var dic = new ResourceDictionary
{
Source = new Uri(stylePath, UriKind.Relative)
};
Application.Current.Resources.MergedDictionaries.Add(dic);
}
When the condition exists to change your StyleSheet, call UpdateStyles.
Note:
The onRedraw parameter is simply a final action to perform after updating the style. In some cases, you may want to pass
this.InvalidateVisual
from the UI if you are having issues with the UI properly updating.

Related

Getting the parent window from the most inner WPF usercontrol

I have an WPF User control, let's say UCInner, which contains a WPF Popup. UCInner is used in another WPF user control, let's say UCOuter.
UCOuter is embedded in an ElementHost (ElementHost.Child = UCOuter).
Finally UCOuter is embedded within an Outlook VSTO custom task pane ahd this latter used in a winforms application (Outlook VSTO Add-in).
So from the most inner WPF Control, UCInner, I would like to obtain the parent Window. I have tried some alternatives with no success, I am always getting null or exceptions:
Window w = Window.GetWindow(myPopup);
Window w = Window.GetWindow(UCInner);
I also have tried what explained here and also this one.
UPDATED -
ANOTHER ATTEMPT:
I have tried below piece of code and i can get successfully the window handle, but now from the handle I need to get the Window Object.
dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
Microsoft.VisualStudio.OLE.Interop.IOleWindow win = activeWindow as Microsoft.VisualStudio.OLE.Interop.IOleWindow;
IntPtr handle;
win.GetWindow(out handle);
So in order to get the Window object I have tried this based on the Window handle:
System.Windows.Interop.HwndSource hwndSource = System.Windows.Interop.HwndSource.FromHwnd(handle);
Window w = hwndSource.RootVisual as Window;
but this does not work, hwndSource is null.
If you need to figure out the right parent window to display your own WPF window, cast Application.ActiveWindow to IOleWindow (Application.ActiveWindow can return either Explorer or Inspector, they both support IOleWindow) and call IOleWindow.GetWindow. Once you have the HWND, create an instance of the WindowInteropHelper class and specify the Outlook window handle as the parent:
if (outlookHwnd != IntPtr.Zero)
{
WindowInteropHelper helper = new WindowInteropHelper(YourDialogWindow);
helper.Owner = outlookHwnd;
YourDialogWindow.ShowInTaskbar = false;
}
First, you need to retrieve the parent window handle, in case of Explorer window in Outlook you can use:
Outlook.Explorer explorer = OutlookApplication.ActiveExplorer();
IOleWindow oleWindow = explorer as IOleWindow;
IntPtr handle = IntPtr.Zero;
oleWindow.GetWindow(out handle);
if (handle != IntPtr.Zero)
{
WindowInteropHelper helper = new WindowInteropHelper(DialogWindow);
helper.Owner = handle;
DialogWindow.ShowInTaskbar = false;
DialogWindow.ShowDialog();
}
where IOleWindow can be defined in the following way:
/// <summary>
/// Implemented and used by containers and objects to obtain window handles
/// and manage context-sensitive help.
/// </summary>
/// <remarks>
/// The IOleWindow interface provides methods that allow an application to obtain
/// the handle to the various windows that participate in in-place activation,
/// and also to enter and exit context-sensitive help mode.
/// </remarks>
[ComImport]
[Guid("00000114-0000-0000-C000-000000000046")]
[InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleWindow
{
/// <summary>
/// Returns the window handle to one of the windows participating in in-place activation
/// (frame, document, parent, or in-place object window).
/// </summary>
/// <param name="phwnd">Pointer to where to return the window handle.</param>
void GetWindow (out IntPtr phwnd) ;
/// <summary>
/// Determines whether context-sensitive help mode should be entered during an
/// in-place activation session.
/// </summary>
/// <param name="fEnterMode"><c>true</c> if help mode should be entered;
/// <c>false</c> if it should be exited.</param>
void ContextSensitiveHelp ([In, MarshalAs(UnmanagedType.Bool)] bool fEnterMode) ;
}

Windows 8 Desktop App: Open tabtip.exe to secondary keyboard (for numeric textbox)

We're working on a desktop WPF app that runs on Windows 7 tablets and are adding some Surface Pro units with windows 8 to the mix.
We noticed immediately that the little keyboard icon no longer displays when a TextBox receives focus. We solved it by running "tabtip.exe" on the MouseDown event for all TextBoxes.
We have some numeric textboxes though (quantity for an item on an order), and want to open the on-screen keyboard for numeric entry, but it opens with qwerty keys by default.
I have been searching extensively for any command-line arguments I can pass to tabtip.exe to change its input mode, but have had no luck. This seems like a trivial task with a metro-style app, but impossible on the desktop side.
Is there a command-line argument to tabtip.exe I can use to accomplish this?
Following on from the answer #tymes provided, here is a quick console app which demonstrates opening the keyboard and changing various settings (C#).:
using System;
using System.Diagnostics;
using Microsoft.Win32;
namespace CSharpTesting
{
class Program
{
/// <summary>
/// The different layout types on the virtual keyboard.
/// </summary>
public enum KeyboardLayoutMode
{
Default,
ThumbLayout,
Handwriting
}
/// <summary>
/// The registry key which holds the keyboard settings.
/// </summary>
private static readonly RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\TabletTip\\1.7");
static void Main(string[] args)
{
SetKeyboardDockedMode(true);
SetKeyboardLayoutMode(KeyboardLayoutMode.ThumbLayout);
ShowKeyboard(true);
}
/// <summary>
/// Shows the onscreen keyboard.
/// </summary>
/// <param name="killExistingProcess">If true, kill any existing TabTip.exe process.</param>
public static void ShowKeyboard(bool killExistingProcess)
{
if (killExistingProcess)
{
// If the user presses the close button on the keyboard then TabTip.exe will still run in the background. If we have made registry
// changes to the keyboard settings, they don't take effect until the process is started again so killing this ensures the keyboard
// will open with our new settings.
foreach (var process in Process.GetProcessesByName("TabTip"))
{
process.Kill();
}
}
string onScreenKeyboardPath = #"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
Process.Start(onScreenKeyboardPath);
}
/// <summary>
/// Sets if the keyboard is in docked or floating mode.
/// </summary>
/// <param name="isDocked">If true set to docked, if false set to floating.</param>
private static void SetKeyboardDockedMode(bool isDocked)
{
registryKey.SetValue("EdgeTargetDockedState", Convert.ToInt32(isDocked), RegistryValueKind.DWord);
}
/// <summary>
/// Changes the layout mode of the keyboard.
/// </summary>
/// <param name="mode">The layout mode to use.</param>
private static void SetKeyboardLayoutMode(KeyboardLayoutMode mode)
{
switch (mode)
{
case KeyboardLayoutMode.Handwriting:
registryKey.SetValue("KeyboardLayoutPreference", 0, RegistryValueKind.DWord);
registryKey.SetValue("LastUsedModalityWasHandwriting", 1, RegistryValueKind.DWord);
break;
case KeyboardLayoutMode.ThumbLayout:
registryKey.SetValue("KeyboardLayoutPreference", 1, RegistryValueKind.DWord);
registryKey.SetValue("LastUsedModalityWasHandwriting", 0, RegistryValueKind.DWord);
// 0 = small, 1 = medium, 2 = large
registryKey.SetValue("ThumbKeyboardSizePreference", 2, RegistryValueKind.DWord);
break;
default:
registryKey.SetValue("KeyboardLayoutPreference", 0, RegistryValueKind.DWord);
registryKey.SetValue("LastUsedModalityWasHandwriting", 0, RegistryValueKind.DWord);
break;
}
}
}
}
in HKEY_CURRENT_USER\Software\Microsoft\TabletTip\1.7 (Windows 8)
change the REG_DWORD KeyboardLayoutPreference
value of 0 is the regular layout
value of 1 is the split keyboard with the numberpad in the middle
the REG_DWORD LastUsedModalityWasHandwriting also has to be 0 or if 1, when tabtip is started again it will open with the pen handwriting area.
You may control input mode by registry setting for Tabtip. Look for the registry entry with name KeyboardLayoutPreference.
I've never used win 8 but in win 10 you can use InputScope to control what on-screen keyboard is used:
<TextBox Grid.Row="0"
InputScope="Number" />
<TextBox Grid.Row="1"
InputScope="Default" />

WPF Best way of displaying a busy indicator when dynamically creating a page

I have a WPF application that runs as an XBAP in a browser. On a few pages all the controls are dynamically created depending on what the user selects. Because of this it can look like the application is not doing anything until all the controls are loaded. I'd like to have some sort of busy indicator displayed before hand to show the user that the controls are loading, it doesn't have to be animated although would be nice if it did. I've looked into the telerik busy indicator but this doesn't work as it's really for getting data for a single control and doesn't show until the controls are loaded which defeats the purpose.
I was thinking of displaying an overlay, or something similar, first, containing a loading logo, then load the page behind this and hide the overlay when the controls have loaded. I was wondering if this was the best way of going about this or if there's a better way?
Note: I haven't tried this in a XBAP browser app, but it works in WPF Apps without any problems!
I use a DispatcherTimer to show an hourglass when necessary, and abstract this code to a static class.
public static class UiServices
{
/// <summary>
/// A value indicating whether the UI is currently busy
/// </summary>
private static bool IsBusy;
/// <summary>
/// Sets the busystate as busy.
/// </summary>
public static void SetBusyState()
{
SetBusyState(true);
}
/// <summary>
/// Sets the busystate to busy or not busy.
/// </summary>
/// <param name="busy">if set to <c>true</c> the application is now busy.</param>
private static void SetBusyState(bool busy)
{
if (busy != IsBusy)
{
IsBusy = busy;
Mouse.OverrideCursor = busy ? Cursors.Wait : null;
if (IsBusy)
{
new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, Application.Current.Dispatcher);
}
}
}
/// <summary>
/// Handles the Tick event of the dispatcherTimer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private static void dispatcherTimer_Tick(object sender, EventArgs e)
{
var dispatcherTimer = sender as DispatcherTimer;
if (dispatcherTimer != null)
{
SetBusyState(false);
dispatcherTimer.Stop();
}
}
}
You would use it like this:
void DoSomething()
{
UiServices.SetBusyState();
// Do your thing
}
Hope this helps!

XNA Folder Hierarchy

Ok so I'm new to XNA and I am just trying to get an image to show on the screen. I believe I have added the Images to the content folder in the VS2010 program HOWEVER when I try to run the program I get an error saying File not found. So I am wondering what folder to have the image in to just be able to call the image file Tank.png.
the code is simple:
namespace Aceldama_Windows_Game
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 mPosition = new Vector2(0, 0);
Texture2D mSpriteTexture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
mSpriteTexture = this.Content.Load<Texture2D>("Tank");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(mSpriteTexture, mPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
What folder should I put the image into to be able to call the file directly without having to put the full file path? Essentially the problem being is it seems as though no matter where i place the tank.png file (whether it is in the the file with the executable/c# files or in the content folder)
The XNA Content Pipeline takes the items that are referenced by your content project and transforms them into XNB files. The ContentManager then loads those XNB files.
So the first thing to check is if the XNB files are being created where you expect them in your output directory.
According to the code you have posted, with Content.RootDirectory = "Content" and Content.Load<Texture2D>("Tank"), assuming a Windows Debug build, it will be looking for the file:
bin/x86/Debug/Content/Tank.xnb
If you changed the content project output directory, you need to change the RootDirectory you set in code as well.
So I figured out what the problem was after searching the web endlessly;p it The code was fine the problem was I needed to create a reference to the content folder in the project. uploaded a pic of the solution explorer to show what I needed to do!
Under Content References I did not have the "Aceldama_windows_gameContent" Added. That needs to be there to reference the game content folder with the images in it!

Pattern for unsaved changes

I'm developing a winforms app with lots of different forms and user controls. Is there a recommended pattern that I could implement that notifies the user that there are unsaved changes on the current form/control when the form/control is exiting and also when the app is closing?
Memento is a way to encapsulate undoable changes.
You can then keep a log of your uncommitted memento instances.
But that's usually way to complex.
State is usually best.
Your application has two "change" states: Saved All Changes, Unsaved Changes.
Each State has a transition rule based on "change" and "save" methods.
The Saved All Changes implementation of "save" does nothing.
The Unsaved Changes implementation of "save" sets the state to "Saved All Changes".
The Saved All Changes implementation "change" sets the state to Unsaved Changes.
The Unsaved Changes implementation of "change" does nothing.
I'm using LLBL Gen pro for the ORM so that has some good entity tracking built into the objects.
I've kind of rolled my own that seems to work pretty well.
I created a new interface that my base User Controls and base Forms implement:
public interface IClosingNotification
{
/// <summary>
/// True if there is a dirty entity (or a dirty entity in the collection) present
/// </summary>
bool DirtyEntityPresent { get; }
/// <summary>
/// Register an entity to be watched for changes
/// </summary>
/// <param name="entity"></param>
void RegisterForClosingNotification(IEntity entity);
/// <summary>
/// Register a collection to be watched for changes
/// </summary>
/// <param name="collection"></param>
void RegisterForClosingNotification(IEntityCollection collection);
/// <summary>
/// Returns true if the form should close without any notification
/// </summary>
/// <returns></returns>
bool ShouldClose();
}
In my base control/form I have a collection of entities that I watch on each form, and I have a CloseForm() method in these classes that I use when a form is closing.
In my forms, whenever I create an object I can then register it for closing notification using:
RegisterForClosingNotification(MyCustomer);
It works well in our scenario.

Resources