c# - Best way to have single View/ViewModel/Xaml link to multiple Json resource files in MvvmCross -
inside application have many pages, have similar layout - header , bodytext.
the way have structured @ moment, every single page have view, viewmodel, .xaml , .json file.
the trouble app has bloated point have 20 pages (don't ask) , copy , pasting these each file inefficient , error prone. i'd have single helpviewmodel, helpview, page_help.xml, , every page different .json file relevant textual resources.
i cannot figure out how achieve despite looking through existing mvvmcross topics.
so xaml has heading looks this:
<textview style="@style/helptexttitle" local:mvxbind="{'text':'path':'textsource','converter':'language','converterparameter':'heading1'}}" /> the json file has same name viewmodel (and gets linked automagic) looks this:
{ "heading1":"sample heading", } and have helpviewmodel can accept argument - name of json file
public string headerfile { get; private set; } public helpviewmodel (string headerfile) { headerfile = headerfile; } the json files associated viewmodels think via class:
public class textproviderbuilder : mvxtextproviderbuilder ... protected override idictionary<string, string> resourcefiles { { var dictionary = this.gettype() .assembly .gettypes() .where(t => t.name.endswith("viewmodel")) .where(t => !t.name.startswith("base")) .todictionary(t => t.name, t => t.name); dictionary[constants.shared] = constants.shared; return dictionary; } } and main app registers being text provider (thus faciliating viewmodel-json file automagic assume) this:
var builder = new textproviderbuilder(); this.registerserviceinstance<imvxtextprovider>(builder.textprovider); so here want somehow use same helppage resources, able bind controls on page either (preferably) different json files same names parameters (header1), or if necessary different parameters inside single json file.
many thanks!
matthew
edit: stuart's solution below works perfectly. have pass name of json file when call requestnavigate comes through on constructor of viewmodel:
return new mvxrelaycommand(() => this.requestnavigate<generalhelpviewmodel>(new { helpkey = "describeaudiocrimestatementhelpviewmodel" })); the thing had create own mvxlanguagebinder, on exact build of mvvmcross (not recent) mvxlanguagebinder private , not accessible.
i copy , pasted version stuart's link , used that:
there several ways achieve type of effect.
if want continue separate json files, go approach like...
change binding use new textsource - customtextsource
<textview style="@style/helptexttitle" local:mvxbind="{'text':'path':'customtextsource','converter':'language','converterparameter':'heading1'}}" /> create helpviewmodel loads customtextsource using navigation parameter:
public helpviewmodel (string helpkey) { customtextsource = new mvxlanguagebinder(constants.generalnamespace, helpkey); } public imvxlanguagebinder customtextsource { get; private set; } change code text resource loading include files.
protected override idictionary<string, string> resourcefiles { { var dictionary = this.gettype() .assembly .gettypes() .where(t => t.name.endswith("viewmodel")) .where(t => !t.name.startswith("base")) .todictionary(t => t.name, t => t.name); dictionary[constants.shared] = constants.shared; foreach (var additional in helpfilelist) { dictionary(additional) = additional; } return dictionary; } } ( i'm assuming here helpfilelist static list perhaps fine way using reflection.)
alternatively, if wanted reorganise json single file, consider writing custom imvxlanguagebinder used helpkey. assuming using vnext, code mvxlanguagebinder should straight-forward adapt - calls axml come line 58
alternatively, if text, consider abandoning xml , json approach - instead using embedded html displayed within web browser widget - might flexible long-term solution allow easiest content update in future.
Comments
Post a Comment