I am trying to create a simple WPF application. However, I am keeping the views, models and bootstrapper in separate class library and calling it from a separate WPF application.
My library has just one view for main window, one MainWindow xaml class and a Bootstrapper derived class. Currently I have overloaded OnStartup
Here is my OnStartup code
protected override object GetInstance(Type serviceType, string key)
{
return base.GetInstance(serviceType, key);
}
Here is my WPF App.Xaml is calling it
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<my:AppBootStrapper x:Key="bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Problem is, I noticed that Bootstrapper object is created but OnStartup is never called. Why ?
Do I have to do anything extra ?
Initialize() is required in the Bootstrap Constructor
Related
I have an app that is using Material extensively. Recently there was an update to Material and looking at the documentation- they have changed how material is initialized. This is the code that I had previously added to my onLaunched method in app.xaml.cs:
this.Resources.MergedDictionaries.Add(new Uno.Material.MaterialColorPalette());
this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("ms-appx:///MaterialColorPaletteOverride.xaml") });
this.Resources.MergedDictionaries.Add(new Uno.Material.MaterialResources());
In looking at the updated documentation at Uno Platform Material How To
The initialization has changed to the following:
Uno.Material.Resources.Init(this, null);
I tried this and Visual Studio tells me that Resources does not exist in the namespace Uno.Material. I also looked at the sample app example and it was similar:
Uno.Material.Resources.Init(this, new ResourceDictionary { Source = new Uri("ms-appx:///MaterialColorPaletteOverride.xaml") });
Obviously it suffers from the same issue- Resources does not exist- the exact error is the method Resources does not exist in Uno.Material. I have verified the other Uno Packages are at the latest. I do have Xamarin.AndroidX.Lifecycle.LiveData installed as well. Before this update to Material- everything was working as expected. The specific update is to 1.0.0-dev.778. I have reverted to 1.0.0-dev.774 and reverted my code to the three lines I first listed- and it is working again as expected. What should I do so I can implement the new changes?
The Uno.Material library recently introduced a breaking change to the way the Material resources are initialized. Going forward, resource initialization should be done via XAML, similar to the way we initialize <XamlControlsResources /> for WinUI.
The documentation is in the midst of being updated but basically you need to move the initialization to your App.xaml like so:
<Application x:Class="Uno.Themes.Samples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:material="using:Uno.Material">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Load WinUI resources -->
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<MaterialColors xmlns="using:Uno.Material"
ColorPaletteOverrideSource="ms-appx:///ColorPaletteOverride.xaml" />
<MaterialResources xmlns="using:Uno.Material" />
<!-- Rest of your application resources .... -->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Notice the new <MaterialColors /> and <MaterialResources /> tags. Keep in mind that order is important here and MaterialColors must be initialized before MaterialResources.
The ColorPaletteOverrideSource is optional, but if you are overriding the default Material colors, you would set it here to the path where your new color palette is defined.
You can then go ahead and remove the calls to Uno.Material.Resources.Init from your App.xaml.cs.
You can have a look at the Uno.Material example for a sample of what your app might look like using the new method of resource initialization.
I am using PRISM with xamarin forms, and I like to declare my viewmodels in the XAML,
xmlns:local="clr-namespace:MyProyect.ViewModels"
……
<ContentPage.BindingContext>
<local:RegistroPageViewModel />
</ContentPage.BindingContext>
so I can have XAML intellicense, in this sample my RegistroPageViewModel constructor have one parameter beacause it need for the base class but I don't know how to pass it within the xaml
public class RegistroPageViewModel : ViewModelBase
{
public RegistroPageViewModel(INavigationService navigationService):base(navigationService)
{
registro = new RegistroInfo();
Title = "Perfil de usuario";
}
My specific question is: How can I still using XAML viemodels's declaration if the viewmodel have a parameter ? how can I pass a parameter in the XAML declaration?
thnaks in advance
For XAML to know about ViewModel, enable XamlC and Compiled Bindings. Documentation provides how to enable and use them properly.
XamlC checks for general compile time errors like property names and open-closing matching tags etc...
Compiled Bindings checks for existence of any property that is bound
You can use the view model locator (ViewModelLocator.AutowireViewModel="True") to have the view model created for you with all dependencies automatically injected.
Setting the view model as design data context (d:DataContext={d:DesignInstance local:RegistroPageViewModel}) should give you intellisense.
I am using Prism to build xamarin forms apps.
I have never used Partial Views and cannot find any examples out there.
Can somebody point me to an example,so that I can see if fits what I am trying to achieve?
many thanks
From the Docs:
The concept of a Partial View is to support a custom layout which may
be reused across multiple pages, and eliminate ViewModel logic
duplication by allowing that custom layout to rely on its own
ViewModel. To use a Partial View you must set the
ViewModelLocator.AutowirePartialView property with a reference to the
containing page as shown here. You should not set the
ViewModelLocator.AutowireViewModel property on the Partial View unless
you are explicitly opting out as setting this property to true
directly may result in the ViewModel being incorrectly set.
Example:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AwesomeApp.Views"
xmlns:prism="clr-namespace:Prism.Ioc;assembly=Prism.Forms"
xmlns:mvvm="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
x:Name="self"
x:Class="AwesomeApp.Views.ViewA">
<StackLayout>
<local:AwesomeView mvvm:ViewModelLocator.AutowirePartialView="{x:Reference self}" />
<Entry Text="{Binding SomeValue" />
</StackLayout>
</ContentPage>
The Caliburn.Micro Xamarin.Forms samples (both setup and features illustrate the use of App.cs with no accompanying App.xaml.
I'd like to have an App.xaml file though so I can add in App-level resources into xaml which are now supposed in Xamarin.Forms. Is this possible? How do I do it?
Note, I haven't added one afterwards ever and this is untested, but it should work something like this.
The App.xaml was introduced later into the Xamarin.Forms packages and templates, probably due to the introduction of the app-level resources you are referring to. But basically, the App.xaml isn't different from any other XAML file.
Add a new App.xaml file which looks somewhat like this:
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="YourNamespace.YourClass">
<Application.Resources>
<!-- Application resource dictionary -->
</Application.Resources>
</Application>
Note that you need to supply your own namespace in the x:Class="YourNamespace.YourClass" attribute. Now in the same class that you specify here, which should be in your App.cs, make sure the class is marked as partial and the InitializeComponent(); is called in the constructor.
Lastly, make sure that your App.xaml file is marked to be an EmbeddedResource as build action.
In fact that is everything there is to it.
If you want to link your App.xaml and App.xaml.cs file so that they are shown as one node in Visual Studio, open your csproj file in a text-editor and find the <Compile Include="App.xaml.cs" /> node. Edit this to be:
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
While this looks nicer, it's not absolutely necessary.
I am working on a winforms project in c#. The project is an outlook plugin, so no hopes of totally converting to mvvmlight/wpf as it's a winforms dll project.
I am however trying to come up with a way to use MvvM Light and WPF with the ElementHost. The issue I have is getting access to the ViewModelLocator. Normally, this gets added in the App.xaml like this:
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
and is available to a view via :
DataContext="{Binding MyViewModel, Source={StaticResource Locator}}"
In my case, I don't have an App.xaml, nor do I have a program main() method where I can bootstrap the framework (as this is a dll project, not an application). I tried the following in my WPF usercontrol (hosted in an elementhost), but it doesn't work:
DataContext="{Binding MyViewModel, Source={StaticResource Locator}}"
...
<UserControl.Resources>
<ResourceDictionary>
<wpf:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
I think it's not working because it's getting declared after its called. This would have been a compromise anyway as I would have needed that code in every view, so I'm kind of glad it didn't work.
Any suggestions how I can get to the ViewModelLocator??
Thanks,
Jeff
So I figured out what to do to solve this. It's not perfect, but it works. In the end, I have no Application to load the bootstrap, so what I really needed was a way to have a view get to the ViewModelLocator so I could databind the view. To do this I made the MyViewModel property static on the ViewModelLocator class and then I changed my DataContext line in the xaml (the view) as follows:
DataContext="{x:Static wpf:ViewModelLocator.MyViewModel}"