c# - Binding a HTML control into a stacklayout using xamarin forms -
being newbie xamrin struggling adding html stacklayout via xamarin forms. have tried quite few things , had google around.
firstly can't work out bindable object supposed using. cannot find straight answer on google/xamarin going assume not easy hoping.
var nameentry = new label (); nameentry.setbinding (label.textproperty, "club.clubname"); var webview = new webview (); webview.setbinding ( ??? , "club.description"); var content = new stacklayout { children = { nameentry, ??? } };
i not sure if possible within xamarin forms itself. can help?
i should point out data form being retrieved asynchronously on remote json endpoint
protected override void onappearing () { base.onappearing (); if (viewmodel == null || viewmodel.isloading) return; viewmodel.loaditemscommand.execute (club.clubid); }
my remote json api contains, description contatins html snippet use.
{ clubname: "stourbridge", description: "<p>this club meets every 2 weeks on <b>friday</b>.</p>" ... }
try following example show how bindings.
note have use htmlwebviewsource
achieve this, , bind webview
.source
this.
clicking button change view model , update webview
appropriately newly changed text.
stacklayout objstacklayout = new stacklayout(); myview objmyview = new myview(); objmyview.myhtml = "<html><head></head><body><h1>title</h1><p>some body text</p></body></html>"; htmlwebviewsource objhtmlwebviewsource = new htmlwebviewsource(); objhtmlwebviewsource.setbinding(htmlwebviewsource.htmlproperty, "myhtml"); objhtmlwebviewsource.bindingcontext = objmyview; webview objwebview = new webview(); objwebview.horizontaloptions = layoutoptions.fillandexpand; objwebview.verticaloptions = layoutoptions.fillandexpand; objwebview.source = objhtmlwebviewsource; button objmybutton2 = new button(); objmybutton2.text="change html"; objmybutton2.clicked+=((o2,e2)=> { objmyview.myhtml = "<html><head></head><body><h1>title</h1><p>some body text has changed.</p></body></html>"; }); objstacklayout.children.add(objmybutton2); objstacklayout.children.add(objwebview);
the view model simple 1 bindable property below:-
public class myview : xamarin.forms.view { public static readonly bindableproperty myhtmlproperty = bindableproperty.create<myview, string>(p => p.myhtml, default(string)); public string myhtml { { return (string)getvalue(myhtmlproperty); } set { setvalue(myhtmlproperty, value); } } }
before clicking button gives:-
after clicking button, adjusts view model, , automatically updates control via binding giving:-
Comments
Post a Comment