How do you add varible in UWP xaml? - xaml

I want to add a variable to my xaml.
My xaml looks like this
<Page
x:Class="UwpTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="using:System"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<system:String x:Key="var1">1.2</system:String>
</Page.Resources>
<Grid>
</Grid>
</Page>
When run, I get runtime error.
Microsoft.UI.Xaml.Markup.ReflectionHelperException
HResult=0x80131500
Message=Error in reflection helper. Please add '<PropertyGroup><EnableTypeInfoReflection>false</EnableTypeInfoReflection></PropertyGroup>' to your project file.. Created Xaml type 'String' has a different name than requested type 'System.String'
Source=Windows
StackTrace:
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
at UwpTest.MainPage.InitializeComponent() in D:\Dev\UwpTest\UwpTest\obj\x86\Debug\MainPage.g.i.cs:line 33
at UwpTest.MainPage..ctor() in D:\Dev\UwpTest\UwpTest\MainPage.xaml.cs:line 27

I want to add a variable to my xaml
You could add x:String into resource as variable for example
<x:String x:Key="var1">Hello</x:String>
For more please refer to XAML intrinsic data types

Related

How to position relative to a child element within another control in XAML

In a UWP app, I have a custom UserControl with some child controls (text boxes, labels, etc.). I have a Page that includes that UserControl as a child element. Now, I want to place a button below that UserControl and align it with one of the control's child text boxes using a RelativePanel. I can't figure out a way to access the child element.
For example, I have a custom control:
<UserControl
x:Class="Sandbox.FooControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Sandbox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBox
x:Name="TheTextBoxIWantToAlignWith"
x:FieldModifier="public"/>
</Grid>
And now I want to align with "TheTextBoxIWantToAlignWith":
<UserControl
x:Class="Sandbox.FooParentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Sandbox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<RelativePanel>
<local:FooControl
x:Name="Foo"
RelativePanel.AlignTopWithPanel="True"
RelativePanel.AlignHorizontalCenterWithPanel="True"/>
<Button
RelativePanel.Below="Foo"
RelativePanel.AlignLeftWith="Foo.TheTextBoxIWantToAlignWith"/> <!-- This doesn't work -->
</RelativePanel>
Obviously this doesn't work ("RelativePanel error: The name 'Foo.TheTextBoxIWantToAlignWith' does not exist in the current context."). I tried:
Setting the FieldModifier to public on the child TextBox
Exposing the TextBox as a public property in code-behind
Exposing the TextBox as a DependencyProperty in code-behind
I really want to avoid flattening out the UserControl into the parent page because it can be reused elsewhere. Is there any way to do this?
I ended up working around the issue with code-behind, similar to Lindsay's suggestion. I hooked into the LayoutUpdated event, which calculated and set the margin. Something like this:
private void AlignButton()
{
var transform = Foo.TheTextBoxIWantToAlignWith.TransformToVisual(RootPanel);
var textBoxRightEdge = transform .TransformPoint(new Point()).X + Foo.TheTextBoxIWantToAlignWith.ActualWidth;
var rightMargin = RootPanel.ActualWidth - textBoxRightEdge;
if (Math.Abs(TheButtonIWantToAlign.Margin.Right - rightMargin) >= 1)
TheButtonIWantToAlign.Margin = new Thickness(0, 0, rightMargin, 0);
}

Imagebrush ResourceDictionary

I've created an Resourcedictionary where i define my ImageBrush to an jpg picture. I use this ImageBrush on my Path object to fill it. However when I build I get the error Error:
Error HRESULT E_FAIL has been returned from a call to a COM component.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:System;assembly=mscorlib"
xmlns:Model="clr-namespace:MVVMTestApp.Model;assembly=MVVMTestApp.Model"
xmlns:View="clr-namespace:MVVMTestApp.View"
xmlns:ViewModel="clr-namespace:MVVMTestApp.ViewModel">
<c:Double x:Key ="StrokeUserControl">
2
</c:Double>
<ImageBrush x:Key="CountryBackground" ImageSource="../Assets/gra-bakgrund.jpg" Stretch="Fill"/>
And I'm trying to get it to fill in my Path object like this
Fill="{StaticResource CountryBackground}"

XAML designer does not see C++/CX classes

Windows Store project in C++/CX. I've placed an instance of the custom C++ class on a XAML page. I've marked it with a namespace. The project builds. Yet the design surface does not display the control, claims it's not present in the assembly. The XAML goes like this:
<UserControl
x:Class="MyApp.Foo"
xmlns:local="using:MyApp"
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"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400" Loaded="OnLoaded">
...
<local:MyControl x:Name="Bar" HorizontalAlignment="Left" VerticalAlignment="Top"/>
The MyControl class is declared in code like this:
[Windows::Foundation::Metadata::WebHostHidden]
public ref class MyControl sealed : Windows::UI::Xaml::Controls::Canvas
{
...
};
It has a public default contructor with no parameters. What's wrong here, please?

How to check which instance of child control is used in silverlight

I have two usercontrols : ParentUserControl.xaml and ChildUserControl.xaml
ParentUserControl:
<UserControl x:Class="DemoSilverlight.ParentUserControl"
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:DemoSilverlight"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
Loaded="UserControl_Loaded">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:TabControl x:Name="tabControlHeatMapEditor" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionChanged="tabControlHeatMapEditor_SelectionChanged" Height="510" FontFamily="Verdana" FontSize="12">
<sdk:TabItem Header="Original Heat Map" x:Name="tabItemOriginalHeatMap">
<Local:ChildUserControl x:Name="chartControlOriginal"/>
</sdk:TabItem>
<sdk:TabItem Header="Revised Heat Map" x:Name="tabItemRevisedHeatMap">
<Local:ChildUserControl x:Name="chartControlRevised"/>
</sdk:TabItem>
<sdk:TabItem Header="Compare Original/Revised" x:Name="tabItemCompareOrgRev">
<!--<Local:ChildUserControl x:Name="chartControlCompare"/>-->
</sdk:TabItem>
</sdk:TabControl>
</Grid>
</UserControl>
Inside the ChildUserControl control I have a button control. In the code behind page of the
ChildUserControl.xaml.cs, I want to know which instance of the ChildUserControl is used and based on that I want to access the button within the ChildUserControl in order to make it disabled.
Can anyone help me to resolve this problem
Thanks & Regards,
Santosh Kumar Patro
Have a enum in ChildUserControl. Based on TabItem Selection set the enum property and further based on your condition you can either enable or disable the button.

how can i set header templete in pivot item at run time

actually i want to set image on header of every pivot item .....at run time using a resource
<UserControl x:Class="WindowsPhoneApplication7.heder"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="18" d:DesignWidth="24" Loaded="UserControl_Loaded">
<UserControl.Resources>
<DataTemplate x:Key="heder_image">
<Image Source="/WindowsPhoneApplication7;component/Images/appbar.feature.settings.rest.png"></Image>
</DataTemplate>
</UserControl.Resources>
it is a resource
now my pivot item are in another cs file
heder dis =new heder();
pivotItem[i] = new PivotItem();
pivotItem[i].Header = (DataTemplate)dis.Resources["heder_image"];
but the header templte r not set ......image r not display
What you're doing wrong here, is that you're setting the Header of a PivotItem to a template, instead of using the Pivot.HeaderTemplate. That way it wouldn't be necessary to generate the items in C# either, but instead databind (via. the Pivot.ItemsSource property) them, and do all the styling in XAML as you're meant to do.
And when that is said, using a image for a Pivot Header, should really be discouraged, as it goes against the platforms default look&feel, and thus against the platforms UI and UX guidelines.
Check if the image's Build Action is set to Content (see properties of the file in VS2010 explorer). If the image is in the same project as the XAML page, use "/Images/appbar.feature.settings.rest.png" instead of "/WindowsPhoneApplication7;component...".
Use this XAML snippet and set your header in XAML:
<controls:Pivot ItemsSource="{Binding MyObjects}" HeaderTemplate="{StaticResource heder_image}">
<controls:Pivot.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>