Adding MVVM Light to SL5 (via NuGet) project breaks Application.Resources in App.xaml - mvvm-light

After I add MVVM light, the app.xaml makes you give the default ResourceDictionary an x:key. Even then I can't seem to get the Styles under MainPage.xaml to resolve to the key I provided. I tried to cheat and use Blend, but it crashes every time I open the modified project.
Steps to repro: Create new project (SL5 Navigation Application), then add MVVM Light via Add Library Package Reference.

Ah. ok... simply move inside the existing node. The error message made me think I needed to name the existing node and create a 2nd node.
> <?xml version="1.0" encoding="utf-8"?> <Application
> x:Class="ValidationClient.App"
> 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:vm="clr-namespace:ValidationClient.ViewModel"
> mc:Ignorable="d"> <Application.Resources>
> <ResourceDictionary>
> <ResourceDictionary.MergedDictionaries>
> <ResourceDictionary Source="Assets/Styles.xaml" />
> </ResourceDictionary.MergedDictionaries> <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
> </ResourceDictionary> </Application.Resources> </Application>

Related

mvvmcross xamarin.forms PictureChooser

I am trying to develop a photo app using the PictureChooser plugin. I see that the sample uses Xamarin.iOS. I've googled for examples where the plugin uses Xamarin.Forms but can't find any. I understand how binding works for labels, text editors, and buttons; however, the binding btw the page's image control and the viewmodel's byte[] has got me stomped.
DAA.UI project:
In CameraPage.XAML:
<Image x:Name="MyImage"
Source="{Binding Bytes, Converter={StaticResource InMemoryImage}}"
Aspect="Fill"
HeightRequest="{OnPlatform iOS=300, Android=250}"
WidthRequest="{OnPlatform iOS=300, Android=250}"
HorizontalOptions="Center" />
In App.XAML:
<?xml version="1.0" encoding="utf-8" ?>
<Application
x:Class="DamageAssessmentApp.UI.App"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="clr-namespace:MvvmCross.Forms;assembly=MvvmCross.Forms"
xmlns:resources="clr-namespace:DAA.UI.Resources"
xmlns:local="using:DAA.UI"
xmlns:nativeValueConverters="using:DAA.UI.NativeValueConverters">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<resources:Colors />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<nativeValueConverters:NativeInMemoryImageValueConverter x:Key="InMemoryImage"/>
</Application.Resources>
</Application>
Value Converter file:
using MvvmCross.Forms.Converters;
namespace DAA.UI.NativeValueConverters
{
public class NativeInMemoryImageValueConverter : MvxNativeValueConverter<MvxInMemoryImageValueConverter>
{
}
}
The compiler can't find MvxInMemoryImageValueConverter in the value converter file.
If you are using MVVMCross you should find an example that works with Xamarin.Forms, in which case a good place to start it's their Github.
Or you have to implement it in each platform and use a DependencyService to get the implementation
Other Alternatives
Xamarin Community Toolkit
Another alternative for a camera App is Xamarin Community Toolkit Camera View. In that same link there is an example. But there are more examples in their Github. This is fully compatible with Xamarin.Forms and brings a little more control over the CameraView
Xamarin.Essentials
Xamarin.Essentials offers the MediaPicker that let's the user upload a photo from the gallery or take a new one. But the action of the photo in handled by the OS, so for you it's like a black box. You call the function, and get the photo.

Xamarin navigation bar has unwanted default icon, impossible to alter or remove

Recently I upgraded my Xamarin project to all the latest DLLs. Now on the navigation bar an icon has appeared on Android (haven't looked at iOS yet). See generic icon:
After spending hours searching and prototyping code I still can't figure out how to change or get rid of the icon! Doing a recursive search for png files in my project finds no such image.
There is a method SetTitleIcon but that method doesn't do anything. Some people talk about a toolbar xaml file in the layout folder, but I have no such file.
I followed this tutorial: https://xamarinhelp.com/xamarin-forms-toolbar/ but can only add icons, it doesn't remove the default icon.
I found this demo project: https://developer.xamarin.com/samples/monodroid/Supportv7/AppCompat/Toolbar but that code is quite different from mine. Do I need to go that route?
Anyway, my main xaml file:
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SharedMobile"
NavigationPage.HasNavigationBar="true"
x:Class="SharedMobile.MainPage">
<MasterDetailPage.Master>
<local:MainPageMaster x:Name="MasterPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage BarBackgroundColor="#DCDCDC">
<x:Arguments>
<local:ActionProcessesPage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
There is little to no mention anywhere else in the project of a navigation bar. I did try to add a NavigationPage.Icon under the Navigation Page. Nothing happened.
I only do Xamarin part-time is my excuse.
One of the main reasons I post on Stack Overflow is often (for some reason) shortly after posting I will figure out or find the answer.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SharedMobile;assembly=MyApp.Mobile"
NavigationPage.TitleIcon="transparent.png"
x:Class="SharedMobile.ActionProcessesPage">
In MainActivity.cs (in the Android project),
protected override void OnCreate(Bundle bundle)
{
...
ActionBar.SetIcon(null);
LoadApplication(new Forms.App());
}
Source

ViewModel is not getting called first time in master details page in Xamarin forms with prism

I am using Master details page in my application. I have used Prism in my application.
First time my details page view model is not getting called and due to this page is showing as empty.
<?xml version="1.0" encoding="UTF-8"?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:InforConcierge.Views"
x:Class="InforConcierge.Views.MainPage">
<MasterDetailPage.Master>
<local:LeftMenuPage x:Name="masterPage" Icon="home.png"/>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage x:Name="NavPage" BarBackgroundColor="{DynamicResource appColor}" >
<x:Arguments>
<local:HomePage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
In master page i have "Home","Contacts" options.
Note: If I switch to another menu item("Contacts") and the select "Home" in menu page, then my view model of Home page is getting called.
You are directly creating an HomePage object from XAML. As far as I know, Prism is not able to intercept this and hence no viewmodel is created. You'll have to navigate to the page in order to enable Prism to auto-resolve the VM. We are loading our main view by calling
NavigationService.NavigateAsync("/MenuViewWrapper/NavigationRootPage/HomePage");
from App.OnInitialized. Please note that MenuViewWrapper (which would be your MainPage) and NavigationRootPage (which can be replaced by NavigationPage in your example) have to be registered for navigation (see here).

Xamarin Froms XAML get color from App Resource

I made a color resource in Xamarin Forms App like this:
<Application ...>
<Application.Resources>
<Color x:Name="ColorLineSeparator">#cccccc</Color>
</Application.Resources>
</Application>
I want to use it in MainPage.xaml like this:
<BoxView
HeightRequest=".5"
HorizontalOptions="FillAndExpand"
BackgroundColor="[HOW TO USE IT HERE?]"/>
Original WPF handle this issue something like this:
<Button Background="{DynamicResource ResourceKey=ColorLineSeparator}" />
However it seems not working in Xamarin Forms Page.
It showing this error:
No property, bindable property, or event found for 'ResourceKey', or
mismatching type between value and property.
First of all you have to declare Application.Resources the right way:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<Color x:Key="ColorLineSeparator">#cccccc</Color>
</ResourceDictionary>
</Application.Resources>
</Application>
All the resources declared this way are actually static:
<BoxView BackgroundColor="{StaticResource ColorLineSeparator}"/>
There is a great official article about this stuff.
P.S.: Enabling XAMLC may help you to identify such mistakes in the future.
Add x:Key in your resource:
<Color x:Key="MyColor">#cccccc</Color>
and use it:
<BoxView
HeightRequest=".5"
HorizontalOptions="FillAndExpand"
BackgroundColor="{StaticResource MyColor}"/>

Design-time binding does not work in caliburn.micro 2.0.2

I want to use design-time binding in WPF with caliburn.micro version 2.0.2.
I made simple test project and add neccessary settings for that:
<Window x:Class="CaliburnTest.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:caliburn="http://www.caliburnproject.org"
xmlns:caliburnTest="clr-namespace:CaliburnTest"
d:DataContext="{d:DesignInstance caliburnTest:MainView, IsDesignTimeCreatable=True}"
caliburn:Bind.AtDesignTime="True">
<Grid>
<TextBlock Name="Test" Width="150" Height="50"/>
As a result I dont see value of textblock in design-time, but I see it after app starts.
Could someone tell me what I am missing here? Maybe some additional settings?

Resources