MvvmCross Rio Binding in Windows 8.1 - binding

I'm making a project using Xamarin + MvvmCross and I've encountered a problem while trying to make rio binding working on Windows 8.1 Store View app.
I've added references to BindingEx for Windows plugin, FieldBinding plugin and ofc to Hot-Tuna Starter Pack. Unfortunately, every time I'm trying to run my app I'm getting the error:
"Failed to assign to property 'mvx.Bi.nd'. [Line: 15 Position: 38]"
(Windows.UI.Xaml.Markup.XamlParseException)
Firsty, I thought that there must be something wrong with my core, so I've created a new project (core & Store UI) but it's all the same... But in WP8 UI project similar approach works like a charm..
Im getting the error using for example this piece of code:
In my core:
using Cirrious.MvvmCross.FieldBinding;
using Cirrious.MvvmCross.ViewModels;
namespace App1.Core.ViewModels
{
public class FirstViewModel
: MvxViewModel
{
public INC<string> Sample = new NC<string>("Hi There!");
}
}
In my Store UI:
<views:MvxStorePage
x:Class="App1.Store.Views.FirstView"
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:mvx="using:mvx"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:Cirrious.MvvmCross.WindowsStore.Views"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBlock FontSize="55" mvx:Bi.nd="Text Sample" />
</StackPanel>
</Grid>
</views:MvxStorePage>

Related

Scrolling in Xamarin Forms SfListView causes NullReferenceException in iOS Main function

I just had a very weird issue in my app.
I was adding Syncfusion's SfListView to some pages in my app and for some reason, if the first thing I do when a page that uses the SfListView loads is scroll the ListView, I get a NullReferenceException...in the UIApplication.Main() function that gets called in the iOS project's Main class. There's nothing special in that class at all, though.
It gets better. If I do something else first, like dragging and dropping the items in the SfListView to reorder them and then I scroll it, it doesn't throw the exception.
What on Earth could be causing this?
Here is the XAML for one of the pages that uses the SfListView:
<?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"
xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
mc:Ignorable="d"
x:Class="Partylist.Views.EventsPage"
Title="Events"
BackgroundColor="White">
<ContentPage.ToolbarItems>
<ToolbarItem IconImageSource="settings_gear.png"
Priority="0"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<!--Main layout of the page-->
<StackLayout>
<!--ListView of the events-->
<syncfusion:SfListView x:Name="EventsListView"
SelectionMode="Single"
SelectionGesture="Tap"
SelectionChanged="OnItemSelected"
DragStartMode="OnHold">
<syncfusion:SfListView.DragDropController>
<syncfusion:DragDropController UpdateSource="True"/>
</syncfusion:SfListView.DragDropController>
<syncfusion:SfListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<SwipeView>
<!--Swipe from the right to make some options
appear-->
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Invoked="OnDelete"
CommandParameter="{Binding .}"
Text="Delete"
BackgroundColor="#ff418b"
IsDestructive="true"/>
<SwipeItem Invoked="OnRename"
CommandParameter="{Binding .}"
Text="Rename"
BackgroundColor="#FF7700"/>
</SwipeItems>
</SwipeView.RightItems>
<!--This is the content that actually appears-->
<StackLayout Padding="20,5">
<Label Text="{Binding EventFolder.Name}"
TextColor="#FF7700"
FontSize="Large"/>
</StackLayout>
</SwipeView>
</ViewCell>
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>
<!--"New Event" button-->
<Button Text="+ Add New Event"
TextColor="#ff418b"
FontSize="Large"
BackgroundColor="#00ffffff"
Clicked="OnNewEventClicked"/>
<!--The banner at the bottom of the screen that gives tips-->
<Frame BorderColor="#ff418b"
Padding="0"
HeightRequest="75">
<FlexLayout Direction="Row"
AlignItems="Stretch"
JustifyContent="SpaceBetween">
<!--The "Tip" icon-->
<Image Source="tip_icon.png"
Margin="10"
FlexLayout.Basis="50"/>
<!--The short version of the tip-->
<Label x:Name="tipLabel"
VerticalTextAlignment="Center"
TextColor="#bb0099"
FontSize="Medium"
FontAttributes="Bold"
FlexLayout.Basis="240"/>
<!--The button that opens up a screen
with the rest of the tip-->
<Button Clicked="OnMoreClicked"
Text="More"
TextColor="White"
FontAttributes="Bold"
FontSize="Medium"
BackgroundColor="#ff418b"
FlexLayout.Basis="100"/>
</FlexLayout>
</Frame>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Here is the iOS project's Main class:
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace Partylist.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate"); // This is the line that throws the exception.
}
}
}
UPDATE: I just applied the SfListView to a couple more pages in my project and I couldn't type into the entries in the items on those pages' lists or use their SwipeViews (the experimental ones in Xamarin, not the ones from the SfListView) without crashing the app in the same way as above.
I just got rid of the Synfcusion ListView and switched back to the one that Xamarin comes with. #BenReierson said it's a problems with Xamarin, so maybe I'll look into switching back when that bug gets fixed.
Glad to inform that the reported issue “NullReferenceException throws when scrolling the SfListView with DragDropController” has been included in Syncfusion's latest Weekly NuGet release update version 18.2.0.46 which is available for download (https://www.nuget.org/).

How do you handle multiple application classes in a xamarin android project

There is a 3rd party library I need to use, that includes a class decorated with the [Application] attribute. This causes compiler errors since I have my own application class that uses an [Application] attribute. I would like my application class to inherit from the 3rd party lib's application class.
public class MyApplication : ThirdPartyApplication
{
}
however since I can't decorate my class with the [Application] attribute I have no way to specify in the Manifest that it should run "MyApplication" and not "ThirdPartyApplication".
If I manually add an entry into AndroidManifest.xml
<application
android:name="com.your.packagename.MyApplication"
android:icon="#drawable/luncher_icon"
android:label="#string/app_name">
It will get replaced after the project gets built with
<application
android:name="mdxxx.ThirdPartyApplication"
android:icon="#drawable/luncher_icon"
android:label="#string/app_name">
Does anyone know how to handle this situation in Xamarin Android?
Keep in mind that the 3rd party library can not be modified.
An alternate solution would be a way to disable all manifest generating attributes and manually create the AndroidManifest. There does not seem to be any way to do this either.
The below post is the exact situation I am having but in pure Android. Note that due to the above issues that this solution will not work for Xamarin.
how-to-handle-multiple-application-classes-in-android
So I was able to figure out a solution but it boarders on being a hack.
In short the android manifest is going to be modified using MSBuild.
Step 1: Have your custom application class inherit from the the Third Party lib's custom application and decorate your class with the "Register" tag.
[Register("com.namespace.MyApplication")]
public class MyApplication : ThirdPartyApplication
{
}
Step 2: Have your project include the RoslynCodeTaskFactory NuGet package
Step 3: Unload your project, then add the following in the Project tag
<UsingTask TaskName="UpdateManifest" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)" Condition=" '$(RoslynCodeTaskFactory)' != '' ">
<ParameterGroup>
<AndroidManifestFilename ParameterType="System.String" Required="true" />
<ApplicationName ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.Xml" />
<Code Type="Fragment" Language="cs"><![CDATA[
XmlDocument doc = new XmlDocument();
doc.Load(AndroidManifestFilename);
XmlNode node = doc.DocumentElement.SelectSingleNode("/manifest/application");
node.Attributes["android:name"].InnerText = ApplicationName;
doc.Save(AndroidManifestFilename);
]]></Code>
</Task>
</UsingTask>
<Target Name="CleanManifest" AfterTargets="_GenerateJavaStubs">
<UpdateManifest AndroidManifestFilename="$(ProjectDir)\obj\$(Configuration)\android\AndroidManifest.xml" ApplicationName="com.namespace.MyApplication" />
</Target>
Step 4: Reload the project and build. The manifest should now point to custom application class. If you get a class not found runtime exception most likely you forgot to add [Register] from Step 1.

Why would Xamarin Forms Activity Indicator not be rendered on iOS?

I am using Xamarin Forms 2.3.2.127 for development of a mobile application.
I have an activity indicator that sits within a StackLayout, which is in turn contained within a Grid Row.
The activity indicator renders on Android, however does not render on iOS.
The controls XAML is as follows:
<ActivityIndicator WidthRequest="50" Color="Red" HeightRequest="50" VerticalOptions="Center" IsVisible="True" IsRunning="True"/>
There are no bindings applied at present, and there have been no changes to the underlying control using CustomRenderers.
Any ideas on what could be causing this issue, or if there are any common bugs? This has occured before in other projects and rectified itself without any changes randomly.
I had same problem & not visible during operation, I m using MVVM Binding, I solved it by invoking the MainThread
XAML
<ActivityIndicator IsVisible="{Binding PBVisibility}" IsRunning="{Binding PBRunning}" Color="Black" />
ViewModel
Device.BeginInvokeOnMainThread (() => {
PBVisibility = true;
PBRunning = true;
RaisePropertyChanged ("PBVisibility");
RaisePropertyChanged ("PBRunning");
});
You run that in MainThread & it will work.

x:Bind Design Time issues

I am trying to build my UWP app and currently am stuck with designer exceptions when trying to use DataTemplate with x:Bind in a Resource Dictionary.
I have created a Resource Dictionary "ItemTemplates.xaml" with a respective code-behind (to ensure x:Bind initialization). The file contains just one template:
<DataTemplate x:Key="HomeViewCategoryListItemTemplate" x:DataType="models:Category">
<Button Background="#88333333" Height="110" VerticalContentAlignment="Top" Padding="10" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock FontWeight="Light" HorizontalAlignment="Center" Text="{x:Bind Name}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" />
<TextBlock Foreground="{ThemeResource ToolTipForegroundThemeBrush}" HorizontalAlignment="Center" Margin="0,10,0,0" Text="{x:Bind Description}" Grid.Row="1" TextAlignment="Center" TextWrapping="Wrap" />
</Grid>
</Button>
</DataTemplate>
Then I added this resource dictionary to App.xaml like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Resources/Core.xaml" />
<resources:ItemTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now the project is unusable, because designer throws weird exceptions, but when I Clean and Rebuild the project and navigate to the HomeView.xaml page, the designer shows just the default "ToString()" items (basically the list view contains just three times the text "Models.Categories") in the ListView and the ItemTemplate property of my ListView is underlined and shows the following error:
The resource "HomeViewCategoryListItemTemplate" could not be resolved.
When I navigate back to App.xaml, I see yet another underline there (of the <resources:ItemTemplates /> line) which says:
The property 'DataType' was not found in type 'DataTemplate'.
Both errors are non-sensical, because when I actually run the app, there are no issues and everything works perfectly. The only workaround I have found so far is to include the ResourceDictionary two times in both the classic way and the "compiled" way:
<ResourceDictionary Source="ItemTemplates.xaml" />
<resoures:ItemTemplates />
This solution works and then everything works both in design time and in run-time, but I really think it is quite messy and there has to be a better, safer approach or I am missing something trivial.
I am running Visual Studio 2015 Update 1 and have the newest UWP SDK installed. The project targets build 10240.
Edit:
Another exception that the designer very often throws and crashes completely:
Unable to cast object of type 'System.String' to type 'Models.Data.Categories.Category'.
According to the StackTrace output this happens inside the ItemTemplates.xaml.cs code - specifically the generated method ProcessBindings. Again, the project still compiles and runs normally, but the designer does not even bother trying to show the output.
For this current version, prefer Binding over x:Bind
an answer from a Microsoft engineers
I've got the same problems as you did, plus a ton of bugs when using x:Bind in Design time. Quickest way to fix: use Binding as old time. And when you release, if performance is under consideration, change the Binding to x:Bind

Bind events to mvvm model in Windows 8

I want to bind event in WebView, but i am having exception:
An exception of type 'Windows.UI.Xaml.Markup.XamlParseException'
occurred in VK.exe but was not handled in user code
WinRT information: Cannot add instance of type
'Win8nl.Behaviors.EventToCommandBehavior' to a collection of type
'System.Collections.ObjectModel.ObservableCollection<WinRtBehaviors.Behavior>'
As you probably know, with Windows SDK there is no System.Interaction.dll. I was trying to find it on the web (compiled for Windows RT) or in Blend folder but could not.
Than i found Win8nl that uses Behaviors to allow me to connect events of controls and RelayCommands in viewmodel.
They helps me to bind loaded event for view:
Here is code:
...
xmlns:WinRtBehaviors="using:WinRtBehaviors"
xmlns:Win8nl_Behavior="using:Win8nl.Behaviors"
DataContext="{Binding Login, Source={StaticResource Locator}}">
<WebView x:Name="webView"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
Height="420" Width="530">
<WinRtBehaviors:Interaction.Behaviors>
<Win8nl_Behavior:EventToCommandBehavior Event="LoadCompleted"
Command="WebViewLoadedCommand" />
</WinRtBehaviors:Interaction.Behaviors>
</WebView>
ViewModel:
public LoginViewModel(IDataService dataService)
{
WebViewLoadedCommand = new RelayCommand(() => {
var msg = new MessageDialog("Web View Loaded");
msg.ShowAsync();
});
}
public RelayCommand WebViewLoadedCommand { get; private set; }
Also i want to get NavigatedEventsArgs parameter, like i was doing for WP7 & System.Interaction:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Navigated">
<mvvm:EventToCommand Command="{Binding NavigatedCommand, Mode=OneWay}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
Thanks!
you want WinRt Triggers.
This open source project implements Interaction.Triggers for WinRT

Resources