Prism Partial views xamarin forms - xamarin.forms

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>

Related

mvvm light Winforms dll project, how can I load the ViewModelLocator

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}"

Caliburn Micro not support cal:View.Context at design-time?

Create window:
<Window
d:DataContext="{d:DesignInstance Type=MyType, IsDesignTimeCreatable=True}"
cal:Bind.AtDesignTime="True">
<ContentControl cal:View.Model="{Binding Model}" cal:View.Context="ViewContext1"/>
</Window>
Create view for view-model named [Root namespace].Model.ViewContext1.
At design time see "Can't find view for view-model".
When run - is all right.
When using Caliburn.Micro in ViewModel first approach, and binding contextual content inside a view to same model (initial ViewModel), make sure to set View.Context before setting View.Model.
You will want to swap them around in this case like so:
<ContentControl cal:View.Context="ViewContext1" cal:View.Model="{Binding Model}" />

In custom WorkflowViewElement, how to support WF4.5 Annotations?

I've created a a custom activity designer, but I wanted to have control over the 'header' part (that by default shows the ModelItem.DisplayName), and as far I know, the only way to achieve this is to derive the designer from WorkflowViewElement rather than ActivityDesigner. So, that seems to work and all is well. Except, I want to support the new Annotations feature of WF4.5.
Given that when you add an annotation to a workflow element on the design surface it adds an icon to the element you are annotating + the actual annotation element, it seems clear that any custom WorkflowViewElement has to have some extra stuff in it to support this behaviour. Can anyone help?
Thanks
As I thought, annotations are saved within the workflow definition as an attached property. Here's what an annotation looks like in the workflow's xaml:
<Sequence
xmlns:derp="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
derp:Annotation.AnnotationText="This is an annotation!">
See, just like any other attached property. Except it isn't. Its an attached workflow property, not an attached DependencyProperty. That means it works through the attached property service as well as the Annotation class. Getting and setting the annotation text on a ModelItem is trivial (and covered below).
Its actually not that hard to support annotations. As long as you don't mind your UI looking like crap. Here's a quick and dirty implementation.
In the UI, add some controls for holding and editing the annotation text
<sap:WorkflowViewElement
x:Class="AnnotationSupport.MyActivityDesigner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
xmlns:ann="clr-namespace:System.Activities.Presentation.Annotations;assembly=System.Activities.Presentation"
xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
x:Name="root"
MinWidth="100"
MinHeight="100">
<Grid Background="red">
<Grid.RowDefinitions>
<RowDefinition
Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Expander
IsExpanded="False">
<!-- HERE SHE BLOWS -->
<TextBox
Text="{Binding ModelItem.AnnotationText}" />
</Expander>
<TextBox
Grid.Row="1"
Text="{Binding ModelItem.Text}"
Margin="10" />
</Grid>
</sap:WorkflowViewElement>
Any time the Annotation text changes, the ModelItem's PropertyChanged event fires, just like any other property. And if you want to grab it from code, the simplest way is to cast the ModelItem to a dynamic:
private void SetAnnotationLol(string newValue)
{
if(ModelItem != null)
((dynamic)ModelItem).AnnotationText = newValue;
}
Now, if you want to create a nice UI like the Fx Activities have... well...
I'll leave it up to you to create a custom Adorner to handle display and editing of the annotation. Which actually isn't as hard as it first looks. If you haven't done one yet, here's your opportunity.

How to pass data to a viewmodel in Caliburn.Micro

This is probably a very simple question, but at this time I have myself so confused I can't see the answer. Simply put, I have a window that contains a content control. I'm using Caliburn.Micro's conventions to "locate" the view.
The window looks like this:
<Window x:Class="Views.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox/>
<ContentControl x:Name="MyViewModel" Height="Auto" Background="Blue"/>
</Grid>
</Window>
The view itself is successfully found, and the screen displays as I expected. However, MyViewModel needs to make a service call to get information based on what is typed into the text box.
So, what I can't seem to figure out is how one would pass that information from the text box to the view model. I've thought of several options, but they all seem to be too much work which makes me think that I'm missing something simple.
Thanks a lot
Like you said there are a number of things you can do:
You could expose a property on MyViewModel and set it within
MainWindowView.
You could use the EventAgregator, publish an event from the
MainWindowView and subscribe to that event from MyViewModel.
Using MEF you could inject a shared resource between the two
ViewModels, set it in MainWindowViewModel, and be able to access it
from MyViewModel.

Entity graphy binding not working in deep level

Suppose I have entity graph like
People ->Student
then in xaml, I have following kind of binding(People is property of VM):
<TextBox Text="{Binding People.Name, Mode=TwoWay}" />
<TextBox Text="{Binding People.Student.StudentNo, Mode=TwoWay}" /> <!-- this bounding is not working -->
in VM, implementing IEditableObject. I have some code like:
public void BeginEdit()
{
((IEditableObject)this.People).BeginEdit();
((IEditableObject)this.People.Student).BeginEdit(); //this code not working
//....
}
When runing the app, all data bound to People is fine.
All data bound to Student is not working.
How to fix it?
I'd guess that the Student property is null on the client side. You need to add the [Include] attribute to the Student property on the server-side, so that it gets taken across to the client side by RIA services. You may also need to add an include for your server-side to retrieve it from the database, depending on how your data-access is written.

Resources