So I recently came across an interesting scenario from one of my teammates.
They are currently working in a Xamarin project and leveraging MvvmCross. They have decided that they would like to use CreateBindingSet exclusively over bindings directly in xaml.
To me this seems like an anti-pattern for Mvvm. It's tightly coupling the behind code to the view model and it adds extra complexity to a platform that is designed to be bound in xaml.
I'm trying to understand if using this function in behind code is a good idea or not but I can't make heads or tails of it from the documentation.
Can someone tell me what the intended use case is for CreateBindingSet?
In addition to providing text-format binding statement which can be easily included in xml layout files, MvvmCross also provides a C# based syntax to enable bindings to be easily constructed using code.
This binding syntax is referred to as Fluent bindings.
For example
MvvmCross creates the relationship between the View and the ViewModel by the following code:
var set = this.CreateBindingSet<MyView, MyViewModel>();
Then you can set the data-binding by For and To methods:
set.Bind(LabelUserName).For(x => x.Text).To(vm => vm.UserName);
With the fluent syntax we can continue to specify the binding type, such as OneWay, and TwoWay, etc, like this:
set.Bind(LabelUserName).For(x => x.Text).To(vm => vm.UserName).TwoWay();
Fluent binding is especially useful in the iOS and OSX platforms where the Xml layout formats are not easily human-editable(in iOS and MACOS platform there is no such a xml file so we need to set binding in code behind).
For more details you could check https://www.mvvmcross.com/documentation/fundamentals/data-binding.
Related
I've been writing a Xamarin Android app and am now in a position where I have a load of generic business logic (including SQLite.Net stuff). I would like to move this to a separate project, in case I decide to have a go at a Xamarin iOS project. Can this just be a vanilla class library project or is there more to it?
This business logic code contains some strings that will need localizing. I guess the only option will be to store them in a resx resource file in the new project?
For the project itself you can use a PCL, this is in fact my favorite option of sharing code between projects in a Xamarin solution or you can go with Shared Projects which I don't like it very much but many people use it and it works. Here's the link I always use as reference when asked which one to use so you can decide yourself based on the pros and cons of each one.
About the localization you can definitely use a resx resource file but you will need to implement your own localization service to read from the files.
Is it possible to link two Core libraries into your app?
I would like to create one Common.Core library that has login and account view models.
I would like another one Domain.Core library that has some domain view models in it.
These could be used across a couple different projects.
In my app, I do a new Setup().Initialize().
My Setup class overrides CreateApp() ...
public class Setup : MvxPhoneSetup
{
protected override IMvxApplication CreateApp()
{
CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
return new Common.Core.App();
// TODO: can I setup a Domain.Core library here too?
}
}
I have tried doing this ...
CreatableTypes(Assembly.Load("Domain.Core")).EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
but I'm getting a ReflectionTypeLoadException when I try to resolve a domain model from there.
Anyone tried something like this?
Thanks!
Yes, using multiple 'core' projects should work.
The ReflectionTypeLoadException occurring on Resolve suggests that maybe your second Assembly requires another Assembly that isn't available? Do you get the same problem with a very simple second core project? Can you get any more information about the exception? Which platform is this occurring on?
If you want to load ViewModel types from multiple assemblies, then there is a Setup method you can override - The default ViewModelLocator in MvvmCross gets its list of ViewModels from the assemblies listed in Setup - see MvxSetup.cs
(Sorry this list is in the ui project - should really be in the main core project)
For cross-platform compatibility, I don't recommend using Assembly.Load - better to use a more static method like typeof(Domain.Core.Something).Assembly
Working on 'packaged application' platforms like xamarin.android and (especially) xamarin.ios I don't recommend using Assembly.Load - this will only work on the iOS platforms if the assembly is referenced statically and has already been loaded - that's the reason plugins have a special bootstrap file on iOS. Also be aware that the name used in Assembly.Load is different on different platforms - eg in Android you must use the filename ending in .dll - see MvxAndroidSetup.cs. For other platforms like WP and winRT, then Assembly.Load may work more conventionally though - although I've personally spent hours/days/weeks swearing at this sort of code in the last year.
I have created my first complex OS X application. While working on it, I've had some doubts about how I use the class that implements the NSApplicationDelegate protocol (the class Xcode creates by default for Cocoa applications, i.e. MyApplicationAppDelegate.m/h).
In many tutorials (and books), I see that people create an AppController class to manage main or generic application tasks. I prefer to add my generic tasks directly into MyApplicationAppDelegate and create specific controllers depending on the modules I need to manage.
For example, I add into MyApplicationAppDelegate every IBAction used to open other windows (i.e. opening a preference panel), every function that isn't strictly connected with a specific module/controller and IBOutlets for the main interface. In MyApplicationAppDelegate I also add every reference to controllers used in my application. That's essentially about it.
I'm really confused because I'm not sure whether or note this is good design. Has MyApplicationAppDelegate been designed for some other purpose?
I would like any suggestions and if possible any articles you might know of about design patterns for Cocoa.
Xcode used not to create an application delegate class in the Cocoa Application template by default.
I believe Apple only introduced the automatic creation of an <AppName>_AppDelegate class with their project template fairly recently, maybe in version 3.2 or so.
This is why many older books and tutorials have you create an AppController class, because the old project template did not create one for you.
You are free to use the <AppName>_AppDelegate as your main controller class, and the reason Apple adds it to their template is that so many developers use the NSApplicationDelegate object as their main application controller.
An excellent resource to learn more about design patterns in Cocoa is the book appropriately called Cocoa Design Patterns.
I'm actively developing desktop applications, local and network services, some classic ASP.NET, etc., so I'm used to static compilation and static code analysis. Now that I'm (finally) learning ASP.NET MVC 3.0 I'm seeing that many of the ASP.NET MVC experts and experienced developers are recommending using strongly-typed views in ASP.NET MVC 3.0 (where applicable).
I'm guessing that "strongly-typed" means writing #model=... at the top of a view's code. But in doing that I only get IntelliSense to work, no static code checking is taking place. I can write anything I want in the #model statement in cshtml and it would compile and run. Consequentially, Model.Anything also compiles. In fact, if I don't type #model I can dynamically use whatever model I want that has "compatible" properties and methods.
I'm used to "strongly-typed" meaning "won't compile", like LINQ to whatever just will not compile if you don't get the properties right. Is there any other purpose for #model other than IntelliSense and a run-time error, and why is it called strong-typed if it's in fact, not?
Strong typing, Meanings in computer literature
Views are compiled at runtime by default. You can modify your project file (csproj) to compile the views when the application builds by setting the following property:
<MvcBuildViews>true</MvcBuildViews>
Downside of this approach is that your buildtime will increase significantly. You should consider only setting this option to true for release builds.
You can edit your project file by unloading the project, right-click the project and choose Edit ProjectFile
It is possible to setup your project so that it includes views in your compilation. This would be where static typing is useful. Another place would be in runtime, if you try to pass in a model that does not match the expected model you will immediately get an exception. If you were to type views dynamically then you would not know your model was invalid until your view tried to access a property of the model and finds out it isn't there.
The second scenario is also a nightmare if you pass in the wrong model object but it happens to have the same named property as the expected model. Then you just get invalid data and debugging becomes hell.
model is new dynamic type in .net 4.0, so these types get resolved at runtime not at compilation time.
Last year I wrote a Language Service for Visual Studio which added syntax highlighting for NHaml files: http://github.com/snappycode/hamleditor.
To clarify, NHaml is a html template language that can mix in code elements like an aspx file can. This plugin adds support to the IDE for editing NHaml files, but basically only adds syntax highlighting.
I was wondering if anyone knows how to add inline c# intellisense to the service like you get now in an aspx file. I'm hoping that would be possible without doing the whole c# grammar myself specific for the plugin.
Has anyone written a language service that mixes languages?
UPDATE:
It looks like the spark view engine guys have made some inroads here, I am investigating their implementation
I checked the Spark View Engine, and they seem to have made a generic ATL stuff (called SparkLanguagePackageLib), that in fact seems to be not containiag anything Spark specific. It seems to be just a generic C# intellisense library that needs the following:
The original code
The C# source that gets generated from the original code
The position mappings between the two (for example the code on line 2 pos 5 gets mapped in the output to line 4 pos 10, etc.)
Some other things, like Paintings(?)
And after that you can call:
events.OnGenerated(
primaryText, // original source code
entry.SourceCode, // generated sourcecode
cMappings, // mappings between the two
ref mappings[0], // ?
cPaints, // ?
ref paints[0]); // ?
I've tried to find Spark-specific stuff in that C++ library, but I couldn't find anything: everythig spark-related is split to a separate C# code file. I think this is good, because:
You don't need to edit the C++ files
If the spark view engine's intellisense support is installed it can be used by other view engines too
You only need to create a class, that maps between the original nhaml file and it's generated C# counterpart.
Btw. Are you still working on this NHaml Intellisense library? If not I'll try to patch their implementation in hope it can be converted to NHaml easily.
this looks like it might help
http://www.codeproject.com/KB/recipes/VSLanguageService.aspx
I finally managed to modify the code to support NHaml. It wasn't that hard at all. Unfortunately the original NHaml library doesn't support everything that was needed, so I had to create a new parser for NHaml. It doesn't support all of the constructs, but it supports most of them (enough to make NHaml programming easier)
Download: http://github.com/sztupy/nhamlsense
Screencast: http://www.youtube.com/watch?v=8jTZ2zC9eYc
You can easily add keywords by creating or modifying a usertype.dat file. Check here for some directions on attaching to specific file extentions. That might get you at least part of the way, without redoing the complete c# syntax.
(In fact, I'm not sure what you mean exactly by 'syntax highlighting' in this context. I'm sure, for instance, you get brace-match highlighting for free in the editor).