I have been trying to publish my WPF app as a standalone .exe instead of as a manifest. I've followed several posts from here and have added the following lines to the .csproj file:
<PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
But that didn't work. So as suggested by another user, I compiled my application as a console app in order to see any errors that are thrown. The console showed these errors:
The only reference to RED.APP is in my AppView.xml file here:
<Application x:Class="RED.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RED">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:Bootstrapper x:Key="Bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I'm using a bootstrapper because I am using CaliburnMicro, here is the bootstrapper class:
using System.Windows;
using Caliburn.Micro;
using RED.ViewModels;
namespace RED
{
public class Bootstrapper : BootstrapperBase
{
public Bootstrapper() //Constructor
{
Initialize();
LogManager.GetLog = type => new DebugLog(type);
//Uses the output window in visual studio to display logging information it collects from Caliburn.Micro
}
//Override the startup method and launch the shell instead
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
}
}
}
Is the error caused by CaliburnMicro? I am not sure what exactly the error is saying. I have resolved a couple of warnings about some references that weren't resolved, and have boiled it down to this last error and am stuck. The event viewer was showing errors 1000 and 1026 when I tried to launch the .exe. I'm not seeing any debug text in the output from Caliburn.
Thanks in advance for the assistance.
Related
I have an WPF application, i using caliburn micro library for MVVM.
public class AppBootstrapper : BootstrapperBase{
protected override void OnStartup(object sender, StartupEventArgs eventArgs)
{
LoadUserConfigData();
if (eventArgs.Args.Count() != 0)
{
MessageBox.Show(eventArgs.Args[0]);
}
else
{
MessageBox.Show("no");
}
StartProgram();
}
}
here is my xaml
<Application x:Class="Mat.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Mat">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:AppBootstrapper x:Key="bootstrapper" />
</ResourceDictionary>
<ResourceDictionary Source="/UIStyle;component/UIStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I using NSIS for creating installer exe file, and i register file associate by NSIS script.
but when i double click on file nothing happen excepted the cursor is loading.
many thanks for your suggest.
i follow #krzysztof skowronek direction, i attach my app process to Visual studio, and debug. the reason it doesn't launch is it got exception that wasn't handled (logged).
I am following a tutorial to implement a value converter. I receive an error for this code:
public class BooleanToVisibilityConverter : IValueConverter
{
//
}
<Application
x:Class="TestApp10.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp10"
RequestedTheme="Light">
<Application.Resources>
<local:BooleanToVisibilityConverter x:Key="TrueToVisibleConverter" />
<local:BooleanToVisibilityConverter x:Key="FalseToVisibleConverter" IsReversed="True" />
</Application.Resources>
I get an error
The name "BooleanToVisibilityConverter" does not exist in the namespace "using:TestApp10"
The function is defined in the App.xaml.cs file.
Thank you.
This seems to be an intellisense error in XAML Designer. Please make sure you have implemented your BooleanToVisibilityConverter correctly. Then you can try with following steps to fix this error.
Open your project in File Explorer, delete bin and obj folder
In Visual Studio, right-click your Solution, and select Clean.
Right-click your Solution, and select Rebuild.
After this, your error should be gone. If you still get this error, you can try to reopen your project in Visual Studio.
I have locator view model:
namespace PassStore.Universal.ViewModels
{
....
public class ViewModelLocator
{
public NewDatabaseViewModel NewDatabase =>
this.container.Resolve<NewDatabaseViewModel>();
}
}
In app.xaml:
..
xmlns:viewModels="using:PassStore.Universal.ViewModels"
..
<Application.Resources>
<!-- Application-specific resources -->
<ResourceDictionary>
<viewModels:ViewModelLocator x:Key="ViewModelLocator" ></viewModels:ViewModelLocator>
</ResourceDictionary>
</Application.Resources>
But I get compile-time error that ViewModelLocator doesn't exist in specified namespace. What can I do?
Try to rebuild your project (right click in Solution Explorer on your project and choose 'Rebuild')
Ok, I busting my head on this for few hours now and still cannot find a solution.
first I shall explain the simple test case I created:
Solution
- ClassLibrary1
- Dictionary1.xaml
- WpfApplication3
- App.config
- App.xaml
- Dictionary2.xaml
- MainWindows.xaml
ClassLibrary1:
That project has the required references to allow me to add wpf-dictionary:
PresentationCore, PresentationFramework, Systam.Xaml, windowsbase
(Along with all standard assemblies for any regular class library)
And this is Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="PrimaryBackgroundColor">#FF030010</Color>
<SolidColorBrush x:Key="PrimaryBackgroundBrush" Color="{StaticResource PrimaryBackgroundColor}" />
</ResourceDictionary>
WpfApplication3:
This project just display a button on a wpf-form.
Dictionary2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1.dll;component/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button">
<Setter Property="Background" Value="{StaticResource PrimaryBackgroundBrush}" />
</Style>
</ResourceDictionary>
MainWindow.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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Content="aaa" Width="100" Height="40" />
</Grid>
</Window>
That's all - very simple as you can see.
The only thing here is that dictionary2 need to use resource from dictionary1.
And so there are two ways to reference another assembly:
Option 1:
The class-library is a project in your solution and your WpfApplication adds reference to the class library project which is in the same solution. this is done via Add-Reference/Projects, And in that situation all works great.
Option 2:
The class-library is not your solution. (actually it can be like in my example)
however you add reference by adding reference to ClassLibrary1.Dll which resides either in your
bin\debug or bin\release folders.
In that situation a portal to hell is opened.
Dictionary2 complains it cannot find the resource 'PrimaryBackgroungBrush' and upon execution it crush
complaining it cannot find the dictionary1.xaml
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
and the inner exception:
{"Could not load file or assembly 'ClassLibrary1.dll, Culture=neutral' or one of its dependencies.
The system cannot find the file specified.":"ClassLibrary1.dll, Culture=neutral"}
The problem is that using option2 is essential as I want to share the same dictionary among other wpf projects without
having the ClassLibrary1 project as part of their solution.
Suggested way to reproduce:
Create a new solution in Visual studio for WPF application.
Add class library project to the solution.
In class libarary project, Add references to the following assemblies: PresentationCore, PresentationFramework, Systam.Xaml, windowsbase
Add Wpf-Dictionary 'Dictionary1' to your class library project and copy the code. (you can copy one from the wpf project since it will not exist as an option in the add item from the class library)
Add Wpf-Dictionary 'Dictionary2' to your wpf application and copy the code.
Copy the code for MainWindow.
And now:
Add reference to class library (as project, from projects tab in add refernce dialog)
Build everything - all should work.
Remove the refernce to class library.
Add reference to class library (as dll, from browse tab and find it in your classlibrary/bin/debug or release folder)
Build everything - you will notice my problem.
Any solution to this problem?
UPDATE 1
I changed the line in dictionary2.xaml from:
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1.dll;component/Dictionary1.xaml"/>
To:
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1;component/Dictionary1.xaml"/>
And now the project compiles and execute without an error, However while in design time - the xaml view of dictionary2 indicate that it cannot find the resource: 'PrimaryBackgroundBrush` and puts the ugly curly underline below it.
So its a progress - but i'm still not happy with that.
Any ideas how to solve that?
UPDATE 2
As previously stated - everything compiles and execute now.
However what you see in the following picture annoys me,
I just want to be sure that others who added the class library as .Dll file and not as project 100% sure they don't get that problem which can be seen in the picture, meaning their xaml intellisense can recognize the resource during design time.
I could imagine how documentation about that dll will looks like:
reference dll in the project
add this to resource dictionary in the project:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1.dll;component/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
add this to each window/usercontrol:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Which looks afwul.
How about making manager in your library which has to be referenced by each window/usercontrol and it will do things automatically?
Here is a cut from theme manager I mentioned in comments (it does merging automatically), think about easy of use.
xaml (add this to each window/usercontrol which has to support theme switching in design/run time):
local:Theme.Theme=""
cs (this part has to be a part of library):
public static class Theme
{
public static readonly DependencyProperty ThemeProperty =
DependencyProperty.RegisterAttached("Theme", typeof(string), typeof(Theme), new PropertyMetadata(null, (d, e) =>
{
var theme = (string)e.NewValue;
// in run-time set theme to specified during init
if (!DesignerProperties.GetIsInDesignMode(d))
theme = _theme;
var element = d as FrameworkElement;
element.Resources.MergedDictionaries.Clear();
if (!string.IsNullOrEmpty(theme))
{
var uri = new Uri($"/MyPorject;component/Themes/{theme}.xaml", UriKind.Relative);
element.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = uri });
}
}));
public static string GetTheme(DependencyObject obj) => (string)obj.GetValue(ThemeProperty);
public static void SetTheme(DependencyObject obj, string value) => obj.SetValue(ThemeProperty, value);
static string _theme = "Generic";
static string[] _themes = new[]
{
"Test",
};
/// <summary>
/// Init themes
/// </summary>
/// <param name="theme">Theme to use</param>
public static void Init(string theme)
{
if (_themes.Contains(theme))
_theme = theme;
}
}
P.S.: functionality is primitive (it is sufficient in my case), but should give you an idea.
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.