Bind events to mvvm model in Windows 8 - webview

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

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/).

MvvmCross Rio Binding in Windows 8.1

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>

Windows Phone Binding

In my RoomView.xaml I have:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox ItemsSource="{Binding myStrings, Mode=TwoWay}"></ListBox>
</Grid>
In my constructor I am doing:
var myStrings = new List<string>{"Usmaan","Carl","Andy","Saul"};
DataContext = myStrings;
Yet nothing is being spat out on the page when I load the application.
Can anyone see where I am going horribly wrong?
The DataContext of your page is already set to the List object, so you just need to set the binding like this:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox ItemsSource="{Binding, Mode=TwoWay}"></ListBox>
</Grid>
Alternatively, you could create an object that has a MyStrings property and use it as the DataContext of the page. Then you could bind the ListBox like you did {Binding myStrings, Mode=TwoWay} while also being able to bind other controls to other properties of that object (that's the principle of ViewModels).

Tilt Effect for HubTile

I added the TiltEffect.IsTiltEnabled property to my HubTile:
<toolkit:HubTile toolkit:TiltEffect.IsTiltEnabled="True" Title="title" Message="This is message" x:Name="name" DisplayNotification="False" Source="pB.png" Tap="tap" />
I also added the HubTile type to the TiltableItems collection in the page's constructor, per the :
public HubPage()
{
TiltEffect.TiltableItems.Add(typeof(HubtTile));
}
but the HubTile don't have tilt effect....
Thanks!
Try adding your HubTile as Content of HyperLinkButton or Button which supports TiltEffect
Add a Style to PhoneApplicationPage.Resources which works for Button and HyperLinkButton that adds support for other controls as Content
<phone:PhoneApplicationPage.Resources>
<Style x:Key="EmptyButtonStyle" TargetType="primitives:ButtonBase">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Padding" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:ButtonBase">
<Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
Also add the primitives namespace to the page
xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows"
Then use your HubTile as HyperLinkButtons's (or Button) Content like this
<HyperlinkButton Style="{StaticResource EmptyButtonStyle}">
<toolkit:HubTile toolkit:TiltEffect.IsTiltEnabled="True" Title="title" Message="This is message" x:Name="name" DisplayNotification="False" Source="pB.png" Tap="tap" />
</HyperlinkButton>
This will finally support TiltEffect and HubTile
Get TiltEffect from Updated Tilt Effect - Peter Torr's Blog
OR download the Silverlight Toolkit Source from Silverlight Toolkit - Downloads
and then add the HubTile class to the list of 'tiltableItems' in TiltEffect.cs:
/// <summary>
/// Default list of items that are tiltable
/// </summary>
static List<Type> tiltableItems = new List<Type>() { typeof(ListBoxItem) , typeof(HyperlinkButton) , typeof(HubTile) };
If you decide to modify the Toolkit, be sure to build the project on Release, and target the .dll you create inside your own application.
I've found other simple solution that works great when HubTile is used as ListBox.ItemTemplate.
Simply set TiltEffect.IsTiltEnabled="True" on ListBox and IsHitTestVisible="False" on HubTile and voila - it works :) You can still respond to HubTile clicks through ListBox.SelectionChanged event and HubTile has working tilt effect.
Hubtile by default is not added to TiltableItems List and needs to be added manullay...
Add the following code to codebehind
public MainPage()
{
InitializeComponent();
TiltEffect.TiltableItems.Add(typeof(HubTile));
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
and add the following line to your hubtile or grid containing the hub tile
<phone:PanoramaItem CacheMode="{x:Null}" Header="item3" toolkit:TiltEffect.IsTiltEnabled="True">

Silverlight 2 - DataContext / Binding problem

I'm having a problem with this this XAML... When I run it, it hangs because of the TextBox. (By "hangs" I mean that the hosting aspx page shows in the browser, but the usercontrol object will not appear on the page, and there are some little green bars in the bottom of the Internet Explorer window that fill up but never go away.) I have both a TextBox and a TextBlock in my code just for testing. It runs fine if I comment out the TextBox and leave only the TextBlock, so I know the DataContext is getting set and the binding to PatternName does work. There are no errors in the Output window to help me debug. Please help! I've spent hours on this problem. What can possible be happening?
<StackPanel x:Name="HolePatternStackPanel" >
<TextBlock Text="{Binding PatternName}" Width="75" />
<TextBox Text="{Binding PatternName}" Height="25" Width="125"/>
</StackPanel>
Here is the code that sets the DataContext from a calling ListBox.SelectionChanged method:
private void lvHolePatterns_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
HolePatternStackPanel.DataContext = this.ActivePattern;
}
Well, I've learned more about this... This whole thing is a Master-Detail UI design, and so I had my ListBox using SelectedItem="{Binding ActivePattern}", and apparently, some infinite loop was getting set up between that and the SelectionChanged eventhandler.
So now my question now becomes what good is SelectedItem anyway? Since I had to add a SelectionChanged eventhandler to update the DataContext of the detail stack panel?
You wouldn't need to use the SelectionChanged event if you set the DataContext of the controls with the SelectedItem
for example
<Grid DataContext="{Binding SelectedItem}">
<TextBlock Text="{Binding some_field_in_selecteditem}" />
</Grid>

Resources