Windows 10 Universal app - xaml strange issues - c#

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')

Related

App wide accessible Flyout in UWP

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}" />

VisualStudios ERROR MainViewModel doesnt exist

Hy Guys i installed VS three times and everytime the same Error
I installed MVVM Light in the NUGET Manager for the MVVM
After installing i set the DataContext in the Main.Window on MainViewModel
That works now. But in my App.xaml is it not possible to get the Resource Dictonary in it. In attachment screenshots of my problem.
appxaml ( <= Full size image)
and...
Try to declare 'vm' namespace before usage, put it in Application tag.
Replace <vm: with <local: as the "local" namespace is aleady mapped at the root element in App.xaml:
<Application x:Class="testapp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:testapp.ViewModel"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:ViewModelLocator x:Key="Locator" />
</Application.Resources>
</Application>

Sharing WPF-Dictionary from another assembly

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.

Adding an existing project to Solution Trouble shooting

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.

Expression Blend 4 accidently deleted App.xaml

Im new to Expression and by accident I deleted the App.xaml file. I think this is an important file and I cannot workout how to create an equivalent.
Please help,
Andy
Create a new project and copy that one.
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Unless you had Application Resources defined, then you may be in trouble.
Might be a good argument for Source Control.
You can create a new Page and call it App.xaml.
Replace its markup as benPearce indicated with this:
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Replace "Test" above with your namespace and "Window1" with the name of the first page you want shown in your project.
Replace the class in the App.xaml.cs code-behind with this:
public partial class App : Application
{
[STAThread]
public static void Main()
{
YourNamespace.App app = new YourNamespace.App();
app.InitializeComponent();
app.Run();
}
}
Ensure that your Project Properties are set in the "Application" area such that your startup object is YourNamespace.App.
Perform a build and you shouldn't get anymore errors related to 'App'.

Categories

Resources