Issue with XAML Design data for ItemsControl - visual-studio-2010

I'm trying to master design data for VS2010 (not Blend) in my MVVM project.
I got it working for top-level VM properties, but have problem with collections. I use DevForce for data modeling, so ideally my XAML should look like this:
<MaintainTruckViewModel
xmlns="clr-namespace:IDATT.Module.Assets.Views"
xmlns:data="clr-namespace:IDATT.Model;assembly=IDATT.Model.SL">
<MaintainTruckViewModel.CurrentItem>
<data:ASTTruck
TruckId="1000"
ExternalTruckId="T1000"
IsActive="True"
VinNumber="1ZAXBV"
Make="Volvo"
Model="ABC"
MakeYear="2010"
PlateNumber="ABC 123"
PlateIssueAdministrativeArea="MO"
Height="110"
AxlesCount="3">
<data:ASTTruck.ASTInsurances>
<data:ASTInsurance
InsuranceKey="1"
CompanyName="MainCo"
AgentCompanyName="Joes insurance"
CoverageAmount = "1000000"
DeductibleAmount = "1000"
InsuranceType = "General liability"
IsActive = "True"
PolicyNumber = "123ABC"
ExpireOn = "01-01-2012"
Note = "This insurance covers all stuff"/>
</data:ASTTruck.ASTInsurances>
</data:ASTTruck>
</MaintainTruckViewModel.CurrentItem>
</MaintainTruckViewModel>
My xaml look like this and I expect to see ASTInsurance data in design view but it doesn't show up
<ItemsControl
Grid.Row="1"
ItemsSource="{Binding CurrentItem.ASTInsurances}">
I don't know how to make various lists to "work" from design data. Any pointers? I found somewhere that I can use separate d:DesignData for list and tried to create such XAML:
<Generic:List
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:Generic="clr-namespace:System.Collections.Generic;assembly=mscorlib"
x:TypeArguments="data:ASTInsurance"
xmlns:data="clr-namespace:IDATT.Model;assembly=IDATT.Model.SL">
<data:ASTInsurance
InsuranceKey="1"
CompanyName="Great West"
AgentCompanyName="Joes insurance"
CoverageAmount = "1000000"
DeductibleAmount = "1000"
InsuranceType = "General liability"
IsActive = "True"
PolicyNumber = "123ABC"
ExpireOn = "01-01-2012"
Note = "This insurance covers all stuff"/>
</Generic:List>
Now XAML editor underlines Generic.List and says The type 'Generic:List' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built

The problem with attempting to use System.Collections.Generic.List<T> in XAML is that (as far as I know) the Silverlight dialect of XAML contains no way to specify values for generic type parameters. The error you're getting is because there is no type System.Collections.Generic.List that does not have a type parameter.
One thing you can do is to create a subclass of List<T> that supplies a value for the type parameter but contains no new nor overridden members, for example:
public class ASTInsuranceList : List<ASTInsurance>
{
}
You can then use an ASTInsuranceList object in XAML to contain a list of ASTInsurance objects.

Related

How to use binding in a label in code behind

I want to display a binding field in a label. It's easy in XAML but how to do it in code-behind? In XAML,
<Label Text="{Binding LastName}" Style="{StaticResource MyLabel}"/>.
In my code behind, I have tried:
Label ln = new Label();
ln.BindingContext = "ContactsModel";
ln.SetBinding = "LastName";
which does not work and I have no clue how to set the Style.
If you read the Basic Binding documentation, then it clearly states that to bind a view you have to.
Specify a BindingContext
Use the SetBinding method to specify the target property to be bound to which ViewModel source property.
The BindingContext may be inferred from the parent element and does not always have to be specified, but your binding should look more like:
var label = new Label();
label.SetBinding(Label.TextProperty, "LastName");
This will bind the Text property on the label to LastName in the ViewModel.

What is the property in control to be set to get the value from AppResult.Text in Xamarin.UITest?

I am trying to create custom control in Xamarin.Forms which has the unique id for automation. So, i have set the android renderer's contentDescription property. So, i can get the AppResult.Label property to identify the control. But, my requirements is that how to get the control's text property? What property i have to set in control level with the corresponding text to get it in AppResult.Text property.
[Test]
[Description("SampleTest")]
public void WelcomeTextIsDisplayed()
{
App.Repl();
AppResult[] results = App.WaitForElement("myControl");
Assert.IsTrue(results[0].Text == "My Control Text", results[0].Text + "\n" + results[0].Description + "\n" + results[0].Id + "\n" + results[0].Label);
}
For more information, I have prepared the simple example to explain better about my case. Here, i have derived my custom control from Grid and i introduced the Text property. When i try to view the element using Repl() method, it does not show the Text property but it shows the text properties for Label & Entry controls.
<StackLayout >
<Label Text="Hello, Custom Renderer!" />
<local:MyEntry Text="In Shared Code" AutomationId="myEntry" />
<local1:CustomView Text="Sample" BackgroundColor="Red" HeightRequest="500" AutomationId="customControl" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
</StackLayout>
public class CustomView : Grid
{
public CustomView()
{
}
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(string),string.Empty);
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
Result while calling App.Repl() ,
I'm not sure how different Xamarin.Forms are to Xamarin.Android (which is mostly what my experience is in.)
What happens if you try
app.Query(c => c.Id("NoResourceEntry-2").Property("Text")).SingleOrDefault();
or some variation of the above? Can you then do something with this? I Hope this helps or points you in the right direction.
Try to use with index position like this:
app.Query(c=>c.Id("NoResourceEntry-2"))[0].Text
similarly you can use class for same:
app.Query(c=>c.Class("LabelRenderer"))[0].Text
Query for Class("LabelRenderer") gave 2 results. So in above example, you can see it gave you 2 results but you have to use index for particular result.

Insert Html file content in xamarin forms

I have one Html file. That html file contains only some div. and i have applied some styles to that all div like margin, color and font-family. Now i want to make a page in my xamarin form app(both in ios and android) and want to add that html file's whole contain in my page.
I have tried by using web view control of xamarin forms. But my html file's content is too long, and because of that, xamarin code is going too long too as we have to applied html as a string to web view control. so i don't want to use that way.
So please give me brief explanation or solution to add html file in xamarin forms.
Hope for better solution.
Thanks in advance.
Not 100% sure what do you mean by "xamarin code is going to long too".
But you can make HtmlWebViewSource object in your ViewModel or in a code-behind, depending what approach you are using and later on you can set or bind WebViews's Source property to it.
In order to set it from code-behind you can do it like this:
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = #"<html><body><h1>Xamarin is awesome</h1></body></html>";
yourWebView.Source = htmlSource;
On the other hand, if you are using MVVM and whole View-ViewModel concept you just need to have a property in your ViewModel of type HtmlWebViewSource and do the simple binding in your View.
Let's say you have the property of type HtmlWebViewSource named HtmlSource, you can set its value to your HTML content like we did in the previous example and than bind to it from WebView control, that should look something like this:
<WebView x:Name="yourWebView" Source="{Binding HtmlSource}" WidthRequest="1000" HeightRequest="1000" />
Hope this was helpful for you, wishing you lots of luck with coding!
If you want to load local html in your contentpage, I suggest you can use DependencyService to get html url from Android Assets file, I create simple in android that you can take a look.
Firstly, creating html in Android--Assets file, name as TestWebPage.html.
Then Creating Interface in Form, IBaseUrl.
public interface IBaseUrl
{
string GetUrl();
}
Then implementing this interface in Android platform:
public class LocalFiles : IBaseUrl
{
public string GetUrl()
{
return "file:///android_asset/";
}
}
ContentPage code:
<StackLayout>
<!-- Place new controls here -->
<WebView
x:Name="webviewjava"
HeightRequest="300"
WidthRequest="300" />
</StackLayout>
public MainPage()
{
InitializeComponent();
var urlSource = new UrlWebViewSource();
string baseUrl = DependencyService.Get<IBaseUrl>().GetUrl();
string filePathUrl = Path.Combine(baseUrl, "TestWebPage.html");
urlSource.Url = filePathUrl;
webviewjava.Source = urlSource;
}
Here is the sample at github, you can take a look:
https://github.com/CherryBu/htmlapp

It is possible to add multiple bindings to a Label using XAML?

It is possible to add multiple Bindings to a Label using XAML, for example:
<Label Text = "{Binding Address} - {Binding City} / {Binding State}" TextColor = "# ffeece" />
No, this is not possible.
But why not concatenate it in your ViewModel and bind to that?
public string Description
{
get { return $"{Address} - {City} / {State}"; }
}
And bind it like: <Label Text = "{Binding Description}" TextColor = "# ffeece" />
I'm not sure if you can add multiple binding to the same property. but you can use like the above answer or use a value converter by passing the object through and returning the formatted string.
If you want to bind different properties on one control in XAML in this situation you have to bind the properties in your view model, then easily you can bind. You can refer above example.

Windows Phone list picker with empty default value

I have scenario where I need to select country from drop down or list picker. I have found how to make this list, but I need to have a default value in this drop-down and I want the default value to be null.
Can someone suggest the best way how to make something like this.
You should bind the SelectedItem value of the drop-down control to a Property in your ViewModel, then avoid setting the property to other than default value in constructor.
This way it will not show any other item, because your SelectedItem property is null.
EDIT:
If I understand you correctly you could do something like this (I'm using Windows Phone Toolkit at http://phone.codeplex.com/
<toolkit:ListPicker ItemsSource="{Binding MyList}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" />
Then your ViewModel could lokk like this:
public ObservableCollection<string> MyList{get;set;}
public string MySelectedItem{
get{ return _mySelectedItem; }
set{ _mySelectedItem = value;
OnPropertyChanged("MySelectedItem");
}

Resources