Strange Visual Studio Behaviour when setting Application Resource Dictionary - c#

I'm trying to set the current WPF Application ResourceDictionary programatically. (I have a WindForms project, so no "App.xaml" to do that for me).
Note: If anyone knows how to bind the equivalent of an Application.Resources to a ElementHost and all of its child controls hierarchy, this is the ultimate objective here.
I added to my code:
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Middlewerks;component/Resources.xaml", UriKind.RelativeOrAbsolute) });
Now it works perfectly in the application, the styling is fine (i.e.: Grids' backgrounds are red. It's just a test style).
But if I open this form in the designer, Visual Studio goes crazy. The whole window uses my style!
Here's a screenshot: http://localhostr.com/files/8368cc/Failure.jpg
The cool part is that I found how to edit the Visual Studio 2010 ugly blue skin. The sad part is that won't make my customers happy when they develop with my control.
Feel free to try it and tell me how I should implement my resources without screwing everything up.
XAML Code: (shown in screenshot)
EDIT: Here is my temporary, very hackish solution so I can keep on developing. It really is a pain that "Application.Current" works on Visual Studio.
if (Application.Current.MainWindow == null || !Application.Current.MainWindow.Title.EndsWith(" - Microsoft Visual C# 2010 Express"))
{
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Middlewerks;component/Resources.xaml", UriKind.RelativeOrAbsolute) });
}

When I worked on a WinForms project that used WPF areas, I just used MergedDictionaries to bring in the resources I needed, whenever I needed them.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DefaultResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Can you get away with that? You can still put a code behind on the dictionary if you need to do more programmatically.
You can use this at any level on any element. That is, it doesn't have to be a Window as shown here.

Related

Xceed Wpf Toolkit works in code but not designer

I am hoping this problem is just me being unfamiliar with WPF.
I am able to use the XCeed toolkit in code with no problems:
DateTimePicker dtp = new DateTimePicker();
dtp.HorizontalAlignment = HorizontalAlignment.Left;
dtp.VerticalAlignment = VerticalAlignment.Top;
dtp.Width = 200;
dtp.Height = 24;
dtp.Margin = new Thickness(50, 10, 0, 0);
dtp.FontStyle = FontStyles.Normal;
InBoundGrid.Children.Add(dtp);
This works fine in my form.
But I cannot figure out how do get it working in the designer:
I've added:
xmlns:wpfx="http://schemas.xceed.com/wpf/xaml/toolkit"
to the Window definition.
It shows fine in "intellisense"
So when I start typing wpfx:... the DateTimePicker contol shows in the intellisense menu.
However:
<wpfx:DateTimePicker
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="200"
Height="24"
Margin="50,10,0,0"
FontWeight="Normal"
/>
comes back with the error:
"The name "DateTimePicker" does not exist in the namespace http://schemas.xceed.com/wpf/xaml/toolkit"
All the other Xceed controls show the same error in xaml designer.
Also, please note I do not want to try the Nuget installer.
(I tried but got the same error anyway)
I would like to keep my code base as small as possible.
Thanks in advance.
Took me awhile, but I finally found a solution:
1) Go To Build|Configuration Manager
2) Under "Active solution platform" select
3) Change to x64
I had the setting set for AnyCPU. I think it is the default for VS.
The designer came back as soon as I set it to x64.
Hopefully this may help anyone else using the Xceed Toolkit in designer.
Your code seems to be fine I don't see anything wrong with it. As long as you are sure that url is the correct one to be including.
For me sometimes Visual Studio is a little funny with the XAML. After adding the resource try to rebuild the solution, or clean it then hit build. This seems to refresh the XAML resources. If this doesn't work try restarting Visual Studio.

wpf c# can't open window

I've created a new window in wpf named AfterLogin.
I'm trying to open it with button like
VMR_Launcher.AfterLogin af = new VMR_Launcher.AfterLogin();
af.Show();
and like that
AfterLogin af = new AfterLogin();
af.Show();
It throws error Cannot locate resource AfterLogin.xaml
I have other windows too, but they working with those commands, and this afterlogin not. idk why.
Every new window doesn't work too.
What I would do, which has solved these types of one off things for me in the past, is create a new window in the folder its going to be in and start to copy the code over. Try copying just some of the code and functionality and build up to all of the code. If you finish copying over forget about ever having a problem and dismiss it as a one off. Its hard for us programmers to give up on figuring out why a problem occurs, but sometimes its better to just solve it the easy way and move on.
Try doing this.
AfterLogin af = new AfterLogin();
af.ShowDialog();
Make sure that AfterLogin.xaml file is there in your project. If it is not then import it.
If it is already there, then there must be one App.xaml file inyour project.
Open it! Inside <Application> tag,there will be <Application.Resources> tag, inside this tag make sure that code similar following exists.
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="AfterLogin.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

WPF C# Application I get 2 main windows

I am really new to WPF and c#, and have some questions about how the applications are initialized. I am going through some tutorials on LINQ classes in the hopes that I can bind all of my SQL Server data with observableCollections.
One of the examples I found has a start like so (in the App.xaml.cs file):
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow app = new MainWindow();
ProductViewModel context = new ProductViewModel();
app.DataContext = context;
app.Show();
}
}
When I run the application I get 2 main windows. The commands above execute and open the first one, but then a second one gets called from some system code which I can't see (I can see in dissasembler, but that does not really help). So it seems that the application that VS set up for me has a standard entry, but the sample code (from Rachel Lim) does not do this. I have searched for differences in various files (like App.g.i.cs which has the void Main() call) and both my application and the sample are the same. I am trying to "take control" of the application to handle creating my observablecollections with the LINQ classes. Is anyone familiar enough with VS and C# to give me some hints on what might be happening?
The problem is probably in your App.xaml which often has the following:
Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
If your App.xaml.cs opens a window in one of the events as you have demonstrated, then you need to make sure to remove that StartupUri="MainWindow.xaml" in your Application element.

NotImplementedException when overriding default phone resources

I'm am adding the following code to the constructor of App.xaml.cs in my WP7 application.
Resources["PhoneBackgroundBrush"] = new ImageBrush
{
ImageSource = new BitmapImage(new Uri("/images/bg.png", UriKind.Relative))
};
after running it I get NotImplementedException once the applicaion is opened.
Any idea of how can we do this?
What I'm trying to achieve is a single theme application like Facebook that always have white theme regardless of phone's theme.
As a note, getter on the resources always work, so it's not that accessing phone resources is totally blocked from code. (I'm using this to determine current theme and accent of phone).
var a = Resources["PhoneBackgroundBrush"]; // this works fine
For simple examples it isn't complicated when you know how, however you shouldn't rename PhoneBackgroundBrush, but create a new key for your resources.
You need to create the resources in App.xaml (or you can pull in MergedDictionaries other places too, but that's more advanced). Test.jpg needs to be a resource file in your project.
In App.xaml:
<Application.Resources>
<ImageBrush x:Key="MyImageBrush" ImageSource="Test.jpg"/>
</Application.Resources>
In a page for example:
Background="{StaticResource MyImageBrush}"
If you're starting to re-template a control (say a Button), that when you need to crack open Blend and modify the default styles of controls. It's pretty well documented, and builds on these same principles...
Basically the reason you're getting a NotImplementedException is because the setter for the Application.Resources is implemented like this:
set
{
throw new NotImplementedException();
}
The reason Facebook is all white, is because they defined their own colour resources, and used them everywhere. I've also made a always-white themed app. It just requires a little extra effort.

What is the standard way to add controls to a TabPage at Design Time?

I am creating my first Windows Forms application, to be deployed on Windows Mobile and I am having some trouble designing a Tabbed Interface.
I had assumed that I could Create a TabControl, then Add some TabPages and then drag Controls on to each Tab Page in turn. This does not appear to be possible and most of the information I see on the web seems to suggest that the controls should be added dynamically at run-time.
Am I missing something obvious here or is this indeed correct?
If you do have to add the controls at runtime then how do people generally manage the design process. Do they create a Custom UserControl for each tab and then add that at runtime?
Design environment (C# Visual Studio 2005, .net 2.0)
Runtime environment (Windows Mobile 6.1)
Update 1
The actual steps taken within visual studio were as follows :-
Select New Project -> SmartDevice -> Windows Mobile 6 Professional -> Device Application
Added a TabControl to Form1. This automatically adds tabPage1 and tabPage2
Update 2
The solution to this is embarrassingly noobish. The TabControl puts the tabs at the bottom of the page, the first thing I was doing was resizing the tab control to a single line which was then hiding the TabPage control.
Currently i don't use Windows Mobile, but i think it works quite the same.
After adding a TabControl to your form you should take a look into the properties and search for TabPages. Here you can add and delete new TabPages to your Control and design it as you like in the designer.
To your question about using UserControls on each TabPage i would definitely say Yes. It makes easier to separate between each page and what will happen on each one.
Also at a last step i am going to move the needed code out of the Designer.cs into my own function (e.g. var tabControl = CreateTabControl() where all of my properties are set. Then i put all my UserControls into an
private IEnumerable<Type> GetAllTypes()
{
yield return typeof(MyFirstControl);
yield return typeof(MySecondControl);
}
and make an
private void CreateTabPages(TabControl tabControl, IEnumerable<Type> types)
{
foreach(var type in types)
{
var control = Activator.CreateInstance(type);
var tabPage = new TabPage();
tabPage.Controls.Add(control);
tabControl.TabPages.Add(tabPage);
}
}
this will then be called by
CreateTabPages(tabControl, GetAllTypes());
With this approach i can easily add another Tab Page with a single line of code and design it in its own scope.
I just opened vs2008 and created a tabcontrol, then I added controls inside using drag and drop in the designer and I didn't found any problem.
The way I use to do it is to create a usercontrol for each tab, But I add the usercontrol to the tab in the designer. (note that the usercontrol will not appear in the toolbox until you generate your solution).
I didn't know why your method are not working. Did you stop your application before try to add the controls?
Good Luck.

Categories