So a few things to establish before I get into the meat of things:
I'm using .NET 4.0 without having the option to upgrade
I'm limited to VS2010 for my IDE
My application uses the MVVM pattern
It involves a launcher application as a gateway to the underlying apps, which is the intended end-user UX flow
The code is split between multiple projects within the same solution
When I launch the problematic application as standalone, it works perfectly fine; It has issues when I launch it from the launcher application though
The launcher launches the application in a separate window, as a separate process
In essence what the underlying app is meant for is generating and filling out reports with different types of clauses to be checked off (Text, numeric, alphanumeric) and have different data fields, due to which there are a handful of templates for the data to be displayed appropriately in the View.
The issue is when my application is putting together the View to show the generated report, it fails to find the Template Picker.
My base project contains reusable user controls and ViewModels that are reused in the report generation apps. This is where the UserControl for the ListBox for each item in the report's list of items is located. This is also where the C# class for the TemplatePicker is located.
My Condition Report project contains the views for the report-builder application, as well as the styles used across this report-builder. It has it's Models and ViewModels stored in separate projects respectively. This is where the XAML definition for the TemplatePicker is located.
This is the code for the CustomListBox where it references my TemplatePicker.
<xmlns:ConditionsPicker="clr-namespace:eSOCs.Base.Helpers;assembly=eSOCs.Base"/>
<ListBox SelectedValue="{Binding Path=DataContext.SelectedCondition}"
Grid.Row="5" Grid.RowSpan="3" Grid.ColumnSpan="9"
Visibility="{Binding Path=DataContext.ConditionsListBoxVisibility, Converter={StaticResource BooleanToVisibilityConverter}}"
ItemsSource="{Binding Path=DataContext.ConditionsForGroup}"
ItemTemplateSelector="{StaticResource ResourceKey=ConditionTemplatePicker}" Margin="1,6,-1,117"
x:Name="ListBoxConditions">
This is the code excerpt from my DataTemplates.xaml which is the xaml definition for my TemplatePicker (ConditionsListTemplatePicker is the c# class for the template picker):
<xmlns:ConditionsPicker="clr-namespace:eSOCs.Base.Helpers;assembly=eSOCs.Base"/>
<ConditionsPicker:ConditionsListTemplatePicker x:Key="ConditionTemplatePicker"
DefaultConditionTemplate="{StaticResource DefualtCondiditonTemplate}"
TextMessageConditionTemplate="{StaticResource TextCondiditonTemplate}"
PolymerConditionTemplate="{StaticResource PolymerSourceTemplate}"
RecoveryConditionTemplate="{StaticResource PolymerSourceTemplate}"
TextConditionTemplate="{StaticResource ConditionTextDataTemplate}"/>
And this is my App.xaml for the report-builder app adding to the ResourceDictionary:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/Colors.xaml" />
<ResourceDictionary Source="/Styles/Fonts.xaml" />
<ResourceDictionary Source="/Styles/Strings.xaml" />
<ResourceDictionary Source="/Styles/Controls.xaml" />
<ResourceDictionary Source="/Styles/Labels.xaml" />
<ResourceDictionary Source="/Styles/Canvas.xaml" />
<ResourceDictionary Source="/Styles/TextBlock.xaml" />
<ResourceDictionary Source="/Styles/Borders.xaml" />
<ResourceDictionary Source="/Styles/ListBox.xaml" />
<ResourceDictionary Source="/Styles/Buttons.xaml" />
<ResourceDictionary Source="/Styles/Windows.xaml" />
<ResourceDictionary Source="/Styles/ComboBox.xaml" />
<ResourceDictionary Source="/Styles/ProgressBar.xaml" />
<ResourceDictionary Source="/Styles/CustomKeypadStyle.xaml" />
<ResourceDictionary Source="/CustomSelectionPopup/CustomPopupTextBoxStyle.xaml" />
<ResourceDictionary Source="/Styles/DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
This is the code that performs the process-creation from within the context of the launcher:
public void BringExeToForeground(string exeLocation, string processName)
{
try
{
Process window = null;
if (!string.IsNullOrEmpty(BaseLine.StandardConditionsConnectionString) && !string.IsNullOrEmpty(BaseLine.EsocsConnectionString) && !string.IsNullOrEmpty(BaseLine.ElogConnectionString))
{
var process = GetProcess(processName);
if (process == null)
{
ProcessStartInfo startArgs = new ProcessStartInfo();
startArgs.FileName = exeLocation;
startArgs.Arguments = BaseLine.LineNumber + " " + BaseLine.DeviceVersion + " " + BaseLine.ApplicationVersion;
Process.Start(startArgs);
}
else
{
BringProcessToFront(process);
}
}
else if (!CheckIfProcessIsAlreadyRunning(processName))
{
window = Process.Start(exeLocation);
}
}
catch (Exception ex)
{
LogSystem.LogSystem.LogMessage(LogSystem.LogSystemMessageTypes.Exception, ex.Message, ex.GetType().ToString(), ex.GetType().Name);
}
}
Despite all of this, I have an exception thrown at runtime because CustomListBox cannot resolve the TemplatePicker resource.
Any help is greatly appreciated!
Related
I created a Window style (WPF) and added it as a dll to my project
this style shows corretly when i run the program but doesn't show up in the designer.
I googled already but none of the solutions there are working
Test 1:
// Window //
Style="{DynamicResource HVE_Window}"
// Window.Resources //
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
Result:
Error: 'Window' TargetType doesn not match type of element 'WindowInstance'
-> But it runs and display correctly there
Test 2:
// Window //
Style="{DynamicResource MyWindow}"
// Window.Resources //
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="MyWindow" TargetType="{x:Type Window}" BasedOn="{StaticResource HVE_Window}" />
Result:
No Error:
Still doesn't display in the designer, still shows up if i run the program
Test 3:
Both versions but added to application resources
How it should look:
How it looks inside the designer:
You can sometimes find that resources from a control library are not loaded at design time, despite whatever you put in app.xaml to try and load the things.
MS created a mechanism for Blend which you can use in visual studio since it's the blend designer.
This uses a "special" resource dictionary called DesignTimeResources.xaml
This will only be used at design time.
Add one to the Properties of your problem exe project.
With exactly that name.
Put all your merges into that.
eg this is one of mine from my MapEditor project that uses numerous resources from UILib. UILib is a control library with all sorts of UI stuff in it.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MapEditor">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/Geometries.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/ControlTemplates.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/FontResources.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/UILibResources.xaml"/>
<ResourceDictionary Source="/Views/Drawing/Terrain/Resources/CityResources.xaml"/>
<ResourceDictionary Source="/Resources/MapEditorResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Unload your csproj ( right click in solution explorer), edit it and find the node for that resource dictionary.
Change it to:
<Page Include="Properties\DesignTimeResources.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>
Reload the project, close and re-open visual studio.
Your styles should now apply.
I have a flyout in my app that I use to add additional contacts into the local database. This flyout is declared as a page resource currently, and does exactly what I need it to, however in order to re-use it on a different page I need to copy both the xaml for the flyout and any attatched events to each page that would require using it (there are several places where a contact might need to be added)
Is there a way to globally define a flyout such that I only need to reference it rather than hardcode it each time? I considered using a ContentDialog (as that can be defined as its own module) but I don't think that would be the right fit
If creating completely in code is not an option you can create a ResourceDictionary with a class in background. First the Styles.xaml:
<ResourceDictionary
x:Class="MyClass.Styles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Flyout x:Key="MyFlyout">
<!-- my flyout code -->
</Flyout>
</ResourceDictionary>
The important thing here is x:Class, where the value needs to be the namespace+name of the related class we're creating now (Styles.cs in that case):
namespace MyClass {
public partial class Styles {
public Styles() {
this.InitializeComponent();
}
// my events from flyout
}
}
Make sure that the class is declared as partial and calls this.InitializeComponent() in the constructor.
Now in your app resources add a reference:
<Application
...
xmlns:myClass="using:MyClass">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<myClass:Styles />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Then you can access the Flyout as a static resource, e.g.:
<Button Flyout="{StaticResource MyFlyout}" />
I have a single Winforms project and multiple WPF projects in one solution. From the Winform application I'd like to open one of the WPF Windows (it's a MetroWindow from Mahapps, if it matters).
After looking at the accepted answer to this stackoverflow question I ended up with this piece of code:
OpenWPFAppButton_Click(object sender, EventArgs e)
{
WPFApp wpfApp = new WPFApp();
ElementHost.EnableModelessKeyboardInterop(wpfApp);
wpfApp.Show();
}
Unfortunately if I click the button a XamlParseException occurs, which points to the first Style="{StaticResource ... }" line in WPFApp.xaml (the Main Xaml File).
Does this mean I cannot open WPF Windows from Winforms that include static resources? Or am I missing something simple here?
EDIT: Here is the content of the App.xaml file:
<Application x:Class="WPFAppProjectName.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
StartupUri="WPFApp.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/ButtonStyles.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/MenuStyles.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/TextBoxStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
It is most likely that some common resources are defined in the App.xaml. That file isn't loaded when running through Windows Forms and thus those resources are unavailable. Hence you get this error.
You could include the resource definitions in the Window.xaml files, or an own common style resource file (aka Resource Dictionary) which is included in every Window.
I'm trying to add https://modernuicharts.codeplex.com/ WPF project into my solution.
I extracted the projects that was needed for wpf
Library - De.TorstenMandelkow.MetroChart.WPF
TestApplications - TestApplication.Shared
- TestApplicationWPF
De.TorstenMandelkow.MetroChart
I right clicked my current solutions and I went to Add -> Add Existing project and added the above projects.
When I set TestAplicationWPF as start up Project, the Project runs completely fine.
My goal is to have a button which will display Modern UI Charts Interface in my other project called "WPF".
WPF - ViewModel
//Button Code
public MainWindow ShowModernUI()
{
return new TestApplicationWPF.MainWindow();
}
It returns this error A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
I checked the inner exception and it gave me "Cannot find resource named 'PageContent' which corresponds to TestAplicationWPF.MainWindow.xaml code
TestAplicationWPF.MainWindow.xaml
<ContentControl Content="{Binding}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ContentTemplate="{StaticResource PageContent}" />
Page Content is from a resource Dictionary within TestAplicationWPF.
My attempt to fix the problem was to create a copy of the resource dictionary within my "WPF" project and it removed the error but nothing shows. May I ask how do I display the MainWindow from TestApplicationWPF
Here's how to fix it:
There are few issues with the library, there used to be a Nuget package but it has been unlisted so I'll show you how to compile and reference the library in your WPF app.
download the file SourceAndTestApplications.zip by clicking Download on the right
extract the MetroChart folder somewhere and open the MetroChart.sln inside it
If you are using VS2013 the following screen will appear:
Nothing to worry about, just press OK and close the Migration Report it opened in IE
Again, if you are under Windoww 8.1, the following will appear:
Just press OK.
Now there's something you need to do for the compilation to succeed:
open Configuration Manager
choose Release Build
untick Build for De.TorstenMandelkow.MetroChart
Building
right-click De.TorstenMandelkow.MetroChart.WPF project and build it
when built, find the DLL in MetroChart\De.TorstenMandelkow.MetroChart.WPF\bin\Release
Reference the project
create a new WPF project
add a reference to De.TorstenMandelkow.MetroChart.dll
XAML :
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:metroChart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart"
xmlns:wpfApplication3="clr-namespace:WpfApplication3"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<wpfApplication3:MyObjectCollection x:Key="MyObjectCollection">
<wpfApplication3:MyObject Category="Category1" Value="100" />
<wpfApplication3:MyObject Category="Category2" Value="200" />
<wpfApplication3:MyObject Category="Category3" Value="300" />
</wpfApplication3:MyObjectCollection>
<metroChart:ResourceDictionaryCollection x:Key="CustomColors">
<ResourceDictionary>
<SolidColorBrush x:Key="Brush1" Color="#FF5B9BD5" />
</ResourceDictionary>
<ResourceDictionary>
<SolidColorBrush x:Key="Brush2" Color="#FFED7D31" />
</ResourceDictionary>
<ResourceDictionary>
<SolidColorBrush x:Key="Brush3" Color="#FFA5A5A5" />
</ResourceDictionary>
<ResourceDictionary>
<SolidColorBrush x:Key="Brush4" Color="#FFFFC000" />
</ResourceDictionary>
<!-- add more values with a different key -->
</metroChart:ResourceDictionaryCollection>
</Window.Resources>
<Grid>
<metroChart:PieChart Palette="{StaticResource CustomColors}"
>
<metroChart:PieChart.Series>
<metroChart:ChartSeries DisplayMember="Category"
ItemsSource="{StaticResource MyObjectCollection}"
ValueMember="Value" />
</metroChart:PieChart.Series>
</metroChart:PieChart>
</Grid>
</Window>
Code-behind:
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow() {
InitializeComponent();
}
}
internal class MyObject
{
public string Category { get; set; }
public int Value { get; set; }
}
internal class MyObjectCollection : ObservableCollection<MyObject>
{
}
}
Result
See https://modernuicharts.codeplex.com/documentation for help.
Case
I've got two assemblies: one holding the application, one acting as library.
The library holds and image resource, say: "Images/image.png" with its build action set to resource.
Library
Next to that, the library contains a SomeStyle.xaml file:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Image x:Key="someKey" Source="/LibraryAssemblyHere;component/Images/image.png" />
</ResourceDictionary>
Application
The application assembly has the App.xaml with:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LibraryAssemblyHere;component/Styles/SomeStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Problem
When starting the application, Visual Studio 2010 gives me the following error:
Failed to create a 'ImageSource' from the text '/LibraryAssemblyHere;component/Images/image.png
With an inner exception:
Cannot locate resource 'images/image.png'.
Question
Simple: What am I doing wrong? I've searched and searched, but nothing helped.
Thanks in advance!