How to load data to user controls at start up? - catel

I'm getting started learning MVVM and Catel and have i have problem how to load two user controls with data from a database.
In short, my mainwindow.xaml contains this two rows, (MainViewModel is "empty")
<catel:StackGrid x:Name="LayoutRoot" Margin="5">
<views:View1 Grid.Row="1" Grid.Column="0" Height="Auto" />
<views:View2 Grid.Row="1" Grid.Column="1" Height="Auto"/>
</catel:StackGrid>
My View1ViewModel loads fine, gets data from database and it displays in View1, however my View2ViewModel does not load.
Should i load my secondview from the firstview or should my rows above do the thing and the error is related to binding in my View2?
Any hints would be nice to get to a newbie.

There is no difference between VM1 and VM2. I see you are using a stackgrid without actually defining any rows / columns. Make sure to define those as well so the view gets actually loaded.
There is no need for you to do anything differently for view 2.

Related

Catel wraps usercontrol content in viewmodel grid even when naming content grid "__catelInnerWrapper"

I'm having a problem creating a viewmodel grid manually in Catel for a Catel usercontrol. The documentation implies that by naming the content grid "__catelInnerWrapper" that the content will not be wrapped with another grid by the ViewModelWrapperService and will use my grid instead. I'd like to do so for visual state management purposes. Using Snoopwpf I can see that the usercontrol is still being wrapped.
(https://i.imgur.com/teSu9La.png)
I tried creating a new Catel project template using Catel 5.8.0 and also 5.9.0 beta 0 using .NET 4.6.1 with a single usercontrol in the mainwindow to test a simplified application and saw the same behavior.
<catel:UserControl x:Class="CatelTest.Views.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:catel="http://schemas.catelproject.com">
<Grid x:Name="__catelInnerWrapper">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="{Binding Title}" />
<Label Grid.Row="1" Content="My User Control" />
</Grid>
</catel:UserControl>
In stepping through ViewModelWrappingService.xaml.cs code it seems the usercontrol content Name property is always resolving to null and thus isn't matched with the InnerWrapperName. Do you know why this might be happening? Thank you
When looking at the source, you are doing the correct thing. If you are 100% sure that:
You are using a correct build (e.g. fully rebuilt app, not a
wrong configuration)
Snoop is correct
Then please report an issue (with minimal repro) at https://github.com/catel/catel/ and we will make sure it will be fixed in Catel 5.9 (currently in beta, about to be released but we will wait for this one).

Xamarin.Forms styles causing WeakReference leak

I've been spending a lot of time trying to track down memory leaks in our Xamarin.Forms app on Android. After a lot of blind alleys and false dawns, I think I may have come across something which is causing the problem.
Using Xamarin Profiler, I can see that as soon as I create a Style and apply it to a control (or in fact just an implicit style), we get Multiple WeakReferences remaining 'Live' - i.e. not garbage collected.
Note that I assume that the objects to which they refer have been GC'd (because the reference to the object is weak), but the WeakReferences themselves are remaining.
Now of course WeakReferences are small I know - but when you have hundreds created on every iteration of a page push/pop, then the memory adds up and we have a significant leak.
Here are the details.
Using Xamarin.Forms 2.3.4.270 (we haven't upgraded because we want to keep with known issues!)
Running on Android - physical device.
App.xaml:
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ProfiledFormsApp2;assembly=ProfiledFormsApp2"
x:Class="ProfiledFormsApp2.App">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Label">
<Setter Property="FontSize" Value="Large" />
<Setter Property="TextColor" Value="Blue" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Page XAML:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage Title="Plain Page" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ProfiledFormsApp2.PlainPage">
<ContentPage.Content>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Core Navigation"/>
<Label Text="Number of items:" />
<Label Text="{Binding ItemsCount}" />
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
When I navigate to the above page and back 3 times, we have the following WeakReference (and related) classes created - the screenshot below is from a Profiler snapshot.
Note we have 55 WeakReferences. Drilling into these shows:
What is interesting is that these WeakReferences seem to be created as part of Behavior and Trigger attaching. Looking at the call tree for the top one gives:
So it appears that the WeakReference was created as part of setting a BindableObject's value and the subsequent setting of the Style.
And it also appears that the WeakReference is still in memory and being referenced by something - the Behavior collection?
Using Profiler, I'm can see that we don't have Labels remaining un GC'd. It seems to be something in the Theme/Behavior/Trigger processing.
I haven't looked at the Xamarin.Forms code on GitHub yet - that might have to be my next action.
Has anyone observed this or got a solution?
I am not sure about the implicit styles to the controls but for the explicit styles you can remove them while page is out of scope OR disappearing.
Example, we applied an explicit style to button
<Button x:Name="btnSave" Style="{StaticResource SaveButtonStyle}" Content="Save"/>
protected override void OnDisappearing()
{
btnSave.Style = null;
base.OnDisappearing();
}
Same can be done with Triggers and Behaviors as well (You can clear them).
I think for the implicit styles, there is something within the code of framework only. We can not identify how default style of control is attached with control as default.

Any idea to create masked text box in uwp

Currently I got a requirement to implement a control something like traditional masked text box. But unfortunately there is no first party control (some third party paid control is available like Component 1) available in uwp. If anybody has any idea to create the same please share.
You can make a TextBox background transparent and have a TextBlock behind it with your watermark text. Bind the visibility to the TextBox Text.IsEmpty property using a BooleanToVisibilityConverter
<Grid Grid.Row="0" Margin="5" Background="White">
<TextBlock VerticalAlignment="Center" Margin="3"
Foreground="SteelBlue"
Visibility="{Binding ElementName=MyTextBox, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}">Type in here...</TextBlock>
<TextBox Background="Transparent"
x:Name="MyTextBox"/>
</Grid>
In your resources:
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
Have a look at PlaceHolderText Property on TextBox control.

How to clone UIElement in Silverlight

Can anybody tell me how to clone UIElement in SILVERLIGHT ?. I dug a lot in the google but I'm not able to find relevant solution, everywhere I'm just getting solution to clone UIElement in WPF, however same is not applicable in the Silverlight.
I need to add the a UIElement in the same grid's next rows on click of a + button eachtime.
Plz help..any answer will be appreciable
Thanks
GK
There is no facility to do that.
In your scenario you can use an ItemsControl (directly or as a ListBox, for example) and provide your to-be-duplicated piece of UI as a datatemplate:
<ItemsControl ItemsSource="{Binding MyData}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding LabelText}" />
</Grid>
<DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
This is the most typical way of doing it, but you will need to know about databinding to do it.
There is also the possibility of factoring out your to-be-duplicated piece of UI in a UserControl and add that programmatically multiple times.

WPF window template resource

I am trying to define a window template that can be used by windows in other assemblies. I define a window template and store it in resource dictionary in some assembly. After, I use this template in other assembly on window definition in XAML. It looks that the template is accepted and I can see updated window in VS-2010 designer but when I add a new control to this window, the control disappears from window but still exists in XAML code. I also tried to apply the same template explicitly and it works well.
Xaml code of generic.xaml in project that contains template definition, the ThemeInfo attribute is set and BuildAction property for this file is Page.
<ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly= {x:Type local:DialogResources}, ResourceId=DialogTemplate}">
<Border Width="Auto" Height="Auto" Name="windowFrame"
BorderBrush="#395984"
BorderThickness="1"
CornerRadius="0,20,20,20"
Background="AliceBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="1" Padding="5" Text="Template Window" FontWeight="Bold"/>
<Border Background="#B5CBEF" Grid.Row="1" CornerRadius="0,0,20,20" >
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
</Grid>
</Border>
</ControlTemplate>
DialogRespources - is an empty class that defined in MyProg.Resources assembly.
Now I use this template in other assembly like this:
In this window I add a button but I can't to see it. When I define the TemplateControl explicitly (without using resource) I can see it.
The other problem is that I get a following designer exception when use TargetType="{x:Type Window}" for template in resource: "'Window' ControlTemplate TargetType does not match templated type 'WindowInstance'." I could not find anything regarding this exception in Google.
Please, help me to understand what is wrong in my code?

Resources