ResourceDictionary in a separate assembly dont works. Error Invalid URI - xaml

i get this error if i try to inovke my ResourceDictionary from a Class Libary. I follow this Post but its dont works for me. I dont know what i do wrong.
Invalid URI: Invalid port specified.
My App.xaml:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="JoinApp.App">
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/App_Libary;component/9.Resource/test.xaml"/>
</Application.Resources>
</Application>
My XAML:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
NavigationPage.HasNavigationBar="false"
ControlTemplate="{StaticResource BaseTemplate}"
x:Class="App_Libary.Profile_Page">
<ContentView ControlTemplate="{StaticResource testcontrol}"/>
</ContentPage>
My ResourceDictionary:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App_Libary._9.Resource.test">
<ControlTemplate x:Key="testcontrol">
<Label Text="this is a test"/>
</ControlTemplate>
</ResourceDictionary>

You could do like below:
create the ResourceDictionary xaml (remove x:Class in the root attribute),for example named mytemp.xaml:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
>
<ControlTemplate x:Key="testcontrol">
<Label Text="this is a test"/>
</ControlTemplate>
</ResourceDictionary>
define in your App.xaml:
i get this error if i try to inovke my ResourceDictionary from a Class Libary. I follow this Post but its dont works for me. I dont know what i do wrong.
Invalid URI: Invalid port specified.
My App.xaml:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="JoinApp.App">
<Application.Resources>
<ResourceDictionary Source="mytemp.xaml"/> // if you have sub folder could use Source="yourfolder/mytemp.xaml"
</Application.Resources>
</Application>
then use in your page.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
NavigationPage.HasNavigationBar="false"
ControlTemplate="{StaticResource BaseTemplate}"
x:Class="App_Libary.Profile_Page">
<ContentView ControlTemplate="{StaticResource testcontrol}"/>
</ContentPage>

Related

Create a controltemplate on a page other than App.xaml and how to call it from other pages

I have to create a template common to all pages, only I would like to avoid creating it in the App.xaml page, but creating it in another page makes me mistake when I call it.
This is how I called the template, the page name is Template.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=netstandard"
xmlns:viewmodels="clr-namespace:GET_SOUND.ViewModels" x:DataType="viewmodels:AppViewModel"
x:Class="GET_SOUND.Template">
<ContentPage.Resources>
<ControlTemplate x:Key="HeaderFooterTemplate">
...
</ControlTemplate>
</ContentPage.Resources>
So is the way I call it
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GET_SOUND.Views.Dashboard"
xmlns:renderers="clr-namespace:GET_SOUND.Renderers"
Shell.NavBarIsVisible="False"
ControlTemplate="{StaticResource HeaderFooterTemplate}">
When I created the template in App.xaml I called it like this and it worked without problems
If what you are looking for is to define your control template in a separate file, than you can do that with a Stand-alone resource dictionaries (follow docs instruction on how to add such file in your VS), which you merge with your App.xaml resources:
App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TemplatesResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
TemplatesResourceDictionary.xaml:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ControlTemplate x:Key="HeaderFooterTemplate">
<Grid BackgroundColor="Red"/>
</ControlTemplate>
</ResourceDictionary>
Note
In this sample, the files TemplatesResourceDictionary.xaml and App.xaml are supposed to be located on the same folder, if it is not the case with your project structure then you have to modify the Source property accordingly.

Xamarin Forms include xaml file

I have a xaml file where I wrote the title.
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="TeenageClubWallet.Templates.Header">
<StackLayout>
<Label Text="Header!" />
</StackLayout>
I want to use this xaml file as a header in other xaml files.
Here's an example. I'm trying to include it to another xaml file:
I added a namespace: xmlns:template="clr-namespace:TeenageClubWallet.Templates"
The "template" tag is now used to include file:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="TeenageClubWallet.Statistics"
xmlns:template="clr-namespace:TeenageClubWallet.Templates">
<ContentPage.Content>
<template:Header/>
<StackLayout>
<Label Text="Content!" />
</StackLayout>
</ContentPage.Content>
The visual studio shows the error: "Error XLS0501 The property 'Content' is set more than once."
How can I include content from one xaml file to another xaml file ?
In this example ContentPage has two elements - template:Header and StackLayout and only one is allowed.
That is the cause of this problem. I haven't gone deeper in evaluating if there are further problems, but in the case they exist and you can't resolve them you should ask a new question.
I fixed it
To fix this, you need to insert the <template:Header/> tag inside the <StackLayout> tag.
Below is the full code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="TeenageClubWallet.Statistics"
xmlns:template="clr-namespace:TeenageClubWallet.Templates">
<ContentPage.Content>
<StackLayout>
<template:Header/>
<Label Text="Content!" />
</StackLayout>
</ContentPage.Content>

How to create Footer which can be used for all the xamarin Form pages?

I am creating an app in which i want the footer which will be common for all xaml pages.I tried solution given on this link xamarin forum
but it is showing error for partial class bcoz I'm using xamarin xaml form page while inheriting the class PageToInherit.If I just use page (only .cs file) then there is no error.
public partial class TodoList : PageToInherit
{
public TodoList()
{
InitializeComponent();
this.Title = "TodoList page";
Button button = new Button();
button.Text = "BUTTON 1";
MainStackLayout.Children.Add(button);
Content = MainStackLayout;
}
}
I think you can take a look to TemplatedPage
Create a ControlTemplate
<?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="Mobile.App">
<Application.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="MainPageTemplate">
<StackLayout>
<Label Text="Header Content" FontSize="24" />
<ContentPresenter />
</StackLayout>
</ControlTemplate>
</ResourceDictionary>
</Application.Resources>
</Application>
Apply the ControlTemplate
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Mobile.MainPage"
ControlTemplate="{StaticResource MainPageTemplate}">
<Label Text="Main Page Content" FontSize="18" />
</ContentPage>
Use TemplateBinding
<ControlTemplate x:Key="MainPageTemplate">
<StackLayout>
<Label Text="{TemplateBinding BindingContext.HeadingText}" FontSize="24" />
<ContentPresenter />
</StackLayout>
</ControlTemplate>
Here the Blog
For those who are still looking for this.
Use ContentView for creating reusable xaml views.
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="xxx.FooterView">
<ContentView.Content>
<RelativeLayout VerticalOptions="End" HorizontalOptions="Fill" HeightRequest="42" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}">
<Label HeightRequest="42" VerticalTextAlignment="Center" Text="Copyright © 2017. All rights reserved." HorizontalTextAlignment="Center" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" />
</RelativeLayout>
</ContentView.Content>
</ContentView>
and use it in your page like any control
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="xxx.Page1" xmlns:local="clr-namespace:xxx;assembly=xxx" >
<local:FooterView VerticalOptions="End" HeightRequest="45"/>
//other views and layouts
</ContentPage>
Hope it helps.

how to user color from global resources?

I want to set my resources on the app.xaml and then use it in the differents views of the app, but when I set the color the app crashes u.u, could someone help me ?
app.xaml
<Application.Resources>
<ResourceDictionary>
<Color x:Key="Primary">#FFC107</Color>
</ResourceDictionary>
</Application.Resources>
use it in a StackLayout
<StackLayout Orientation="Vertical" BackgroundColor="{StaticResource Primary}">
Has you called InitializeComponent in the App.xaml.cs?
you need to use Static Resource,I find a good resource for you:
https://blog.xamarin.com/easy-app-theming-with-xamarin-forms-styles/
So you need to do the following:
1- Define a ResourceDictionary at the app-level in App.xaml
<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MonkeyTweet.App">
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">#33302E</Color>
<Color x:Key="textColor">White</Color>
</ResourceDictionary>
</Application.Resources>
</Application>
2- Use StaticResource markup extension to reference predefined resources:
<Label Text="{Binding Text}" TextColor = "{StaticResource textColor}"/>
BackgroundColor="{DynamicResource Primary}"
its a DynamicResource, not a StaticResource.
Here's what my code looks like for example:
App.xaml has
<Color x:Key="titleColor">Green</Color>
and a page.xaml has
TextColor="{DynamicResource titleColor}"

Using MasterDetailPage or ToolbarItem throws exception on Xamarin.Forms

I have the following two pieces of XAML code in a Xamarin.Forms Portable Library XAML project:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.Views.TestView">
<ContentPage.ToolbarItems>
<ToolbarItem Text="{Binding ToolbarItemText}" Command="{Binding ToolbarItemCommand}" Order="Default" Priority="0"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
and
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.Views.Test2View">
<MasterDetailPage.Master>
<ContentPage Title="Currencies">
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<ContentPage>
</ContentPage>
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
When running the Windows Phone 8.1 project (WinRT, not Silverlight), I have the following exception:
E_UNKNOWN_ERROR
Error HRESULT E_FAIL has been returned from a call to a COM component.
The problem is that, you HAVE TO specify the icon paths for the MasterDetailPage.Master page and for the ToolbarIcon. Additionally for the MasterDetailPage.Master page, you have to specify the Title property.
Here are the corrected code samples:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.Views.TestView">
<ContentPage.ToolbarItems>
<ToolbarItem Text="{Binding ToolbarItemText}" Command="{Binding ToolbarItemCommand}" Order="Default" Priority="0">
<ToolbarItem.Icon>
<OnPlatform x:TypeArguments="FileImageSource"
Android="my-icon"
WinPhone="Assets/my-icon.png" />
</ToolbarItem.Icon>
</ToolbarItem>
</ContentPage.ToolbarItems>
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
and
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.Views.Test2View">
<MasterDetailPage.Master>
<ContentPage Title="Currencies">
<ContentPage.Icon>
<OnPlatform x:TypeArguments="FileImageSource"
Android="my-icon"
WinPhone="Assets/my-icon.png" />
</ContentPage.Icon>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<ContentPage>
</ContentPage>
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>