mvvmcross - How to implement Single view for multiple ViewModels Or Single ViewModel for multiple Views - mvvmcross

How to implement Single view for multiple ViewModels
Or Single ViewModel for multiple Views in MvvmCross
Thanks

public partial class App : MvxApplication
{
protected override void RegisterSetup()
{
//this.RegisterSetupType<MvxWpfSetup<Core.App>>();
// 注册自定义设置
this.RegisterSetupType<Setup>();
}
}
public class Setup : MvxWpfSetup
{
protected override IMvxApplication CreateApp()
{
return new Core.App();
}
protected override void InitializeViewLookup()
{
base.InitializeViewLookup();
// 自定义 视图& VM之间的关系
var container = Mvx.IoCProvider.Resolve<IMvxViewsContainer>();
var viewModelViewLookup = new Dictionary<Type, Type>()
{
{ typeof(NativeViewModel), typeof(Test) },
{ typeof(NestedChildViewModel), typeof(Test) }
};
//container.Add(typeof(NativeViewModel), typeof(Test));
//container.Add(typeof(NestedChildViewModel), typeof(Test));
container.AddAll(viewModelViewLookup);
}
}

Related

Construction of generic type parameter in Haxe

I'm trying to instantiate a class based on a function type parameter.
Although the documentation says it is possible, I can't make it work.
Consider the following code:
// Dialog base class
// Every dialog in my application will derive from this
class Dialog
{
public function new()
{
// do some stuff here
}
}
// One of the possible dialogs in the application
// Extends Dialog
class TestDialog extends Dialog
{
public function new()
{
super();
// do some more stuff
}
}
// A simple class that tries to instantiate a specialized dialog, like TestDialog
class SomeAppClass
{
public function new()
{
var instance = create(TestDialog);
}
#:generic
function create<T:Dialog>(type:Class<T>):T
{
return new T();
}
}
This doesn't work with the following error:
create.T does not have a constructor
Clearly, I'm doing something wrong, but what?
SpecialDialog could have a different constructor than Dialog.
So you have to constraint it and then also constraint to Dialog.
Code # Try Haxe
package;
typedef Constructible = {
public function new():Void;
}
// Dialog base class
// Every dialog in my application will derive from this
class Dialog
{
public function new()
{
trace("dialog");
}
}
class SuperDialog extends Dialog
{
public function new()
{
super();
trace("super dialog");
}
}
// A simple class that tries to instantiate a specialized dialog, like TestDialog
class SomeAppClass
{
public function new()
{
var dialog = create(Dialog);
var superDialog = create(SuperDialog);
}
#:generic
public static function create<T:(Constructible,Dialog)>(type:Class<T>):T
{
return new T();
}
}
class Test {
static public function main() {
new SomeAppClass();
}
}

Linking a massive amount of string keys to classes using a static dictionary-like class

Currently, I have two classes MC_Dictionnary_Bad, and MC_Dictionnary_Good, initialized like so:
package classes
{
import flash.utils.Dictionary;
public dynamic class MC_Dictionnary_Bad extends Dictionary
{
public function Custom_Dictionary()
{
this["monster,0,0,0"] = "Monster_Light_Swanp_Red";
this["monster,0,0,1"] = "Monster_Light_Swanp_Blue";
this["monster,0,0,2"] = "Monster_Light_Swanp_Yellow";
this["monster,0,0,3"] = "Monster_Light_Swanp_Dark";
...
}
}
}
They are initialized during loading, and are saved like so during the entire game.
I need to link a name and three indexes to class names, so I can create the appropriate class when needed, given user input.
However, I don't want to use a dynamic class.
Is there a clean way not to use a dynamic class here?
There's no need to extend Dictionary for this model - simply have two dictionaries in a class:
package {
import flash.utils.Dictionary;
public final class Game {
protected var MC_Dictionnary_Bad:Dictionary = new Dictionary();
protected var MC_Dictionnary_Good:Dictionary = new Dictionary();
public function Game() {
initialize();
}
protected function initialize():void {
MC_Dictionnary_Bad["monster,0,0,0"] = "Monster_Light_Swanp_Red";
MC_Dictionnary_Bad["monster,0,0,1"] = "Monster_Light_Swanp_Blue";
MC_Dictionnary_Bad["monster,0,0,2"] = "Monster_Light_Swanp_Yellow";
MC_Dictionnary_Bad["monster,0,0,3"] = "Monster_Light_Swanp_Dark";
}
}
}
If your keys are always strings, this could be defined as JSON which would also enable easy data loading. Instead of embedding the object in your app, it could be loaded from a url and parsed.
package {
public final class Game {
protected var monsters:Object = {
"good": {
"monster": {
0: {
0: {
0: "Monster_Light_Swanp_Red",
1: "Monster_Light_Swanp_Blue",
2: "Monster_Light_Swanp_Yellow",
3: "Monster_Light_Swanp_Dark"
}
}
}
},
"bad": {
/* etc... */
}
};
public function Game() {
// Example accessor:
trace(monsters["good"]["monster"][0][0][1]);
}
}
}

AS3: Type was not found or was not a compile-time constant

I'm having an issue with a little app I'm trying to create at the moment, it's my first try and dealing with classes but for some reason I can't create any instances of the class even if it's imported into the document. Here's the code for the class (named "Players"):
package
{
public class Player
{
public function Player(name_:String)
{
}
public var name_:String;
private var alignment:int;
public var healed:Boolean = false;
public var revealed:Boolean = false;
public var attacked:Boolean = false;
public var dead:Boolean = false;
public function action(target:Player)
{
}
public function describe():String
{
}
}
public class Citizen extends Player
{
public function Citizen(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
}
override public function describe():String
{
return "Citizen";
}
}
public class Investigator extends Player
{
public function Investigator(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.revealed = true;
}
override public function describe():String
{
return "Cop";
}
}
public class Doctor extends Player
{
public function Doctor(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.healed = true;
}
override public function describe():String
{
return "Doctor";
}
}
public class Mafioso extends Player
{
public function Mafioso(name_:String)
{
alignment = -1;
}
override public function action(target:Player)
{
target.attacked = true;
}
override public function describe():String
{
return "Mafia";
}
}
}
And the code which creates the instance:
import Players;
stop();
var totalplayers:Number;
var playerArray:Array = new Array();
var playerType:Array = ["Citizen","Cop","Doctor","Mafia"];
var test:Citizen = new Citizen("James");
Both are in the same folder. I get the error code 1046 described in the title but I honestly have no idea why, flash picks it up in the code hints yet it comes up with that! Any help would be appreciated.
Also secondary question, I'll never initiate the Player class (except through inheritance with the other classes), so can I make it private?
Thanks
I'm assuming all that code is in a file called Players.as.
This is wrong. Each file should contain one class and the class should be the same name as the .as file.
You currently have two classes (Player and Citizen) within one file.
What you need to do is take the Player class you've defined and place it in its own .as. file with the same name (Player). Do the same for Citizen.
Then you can use:
import Player;
import Citizen;
Though this won't be necessary because you don't need to import classes that are in the same directory that you're trying to access it from.
As for the error, you're getting that because Flash is trying to find the class Players and you don't have a class with that name (just a file with that name).
Per your secondary question regarding whether ActionScript supports private classes, if you have a class that would not otherwise be accessed except internally by a public class you may define it as internal.
Internal classes are visible to references inside the current package.
If you do not want a class to be publicly visible outside a package, place the class inside a package and mark the class with the internal attribute. Alternatively, you can omit both the internal and public attributes, and the compiler automatically adds the internal attribute for you. You can also define a class to only be visible inside the source file in which it is defined. Place the class at the bottom of your source file, below the closing curly bracket of the package definition.
In the following example, both X and Y classes are defined in a single file (X.as). X may be referenced and instantiated as normal; however, Y is internal to X and only visible from from the scope of X.
package
{
import flash.display.Sprite;
public class X extends Sprite
{
public function X()
{
super();
var y:Y = new Y();
}
}
}
internal class Y
{
public function Y()
{
trace("internal Y ctor.");
}
}
This pattern is helpful when a class requires small data models that would not otherwise be accessed outside of a class.
Agree with others here should be as shown below (note filenames match class names, file names are denoted in brackets above code blocks). Also you wrote import Players instead of import Player, regardless as the other poster wrote if all classes are currently in the default package the import is unnecessary.
[Player.as]
package
{
public class Player
{
public function Player(name_:String)
{
}
public var name_:String;
private var alignment:int;
public var healed:Boolean = false;
public var revealed:Boolean = false;
public var attacked:Boolean = false;
public var dead:Boolean = false;
public function action(target:Player)
{
}
public function describe():String
{
}
}
}
[Citizen.as]
package
{
public class Citizen extends Player
{
public function Citizen(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
}
override public function describe():String
{
return "Citizen";
}
}
}
[Investigator.as]
package
{
public class Investigator extends Player
{
public function Investigator(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.revealed = true;
}
override public function describe():String
{
return "Cop";
}
}
}
[Doctor.as]
package
{
public class Doctor extends Player
{
public function Doctor(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.healed = true;
}
override public function describe():String
{
return "Doctor";
}
}
}
[Mafioso.as]
package
{
public class Mafioso extends Player
{
public function Mafioso(name_:String)
{
alignment = -1;
}
override public function action(target:Player)
{
target.attacked = true;
}
override public function describe():String
{
return "Mafia";
}
}
}
It's unfortunate there's no abstract classes as this would be an ideal situation for an abstract class and abstract methods.

VAADIN client component logic

I use VAADIN framework in my simple application.
I have my 2 custom components e.g.
#ClientWidget(value = VComponent1.class)
public class Component1 {
private Component2 cmp2;
public void setDataSource(Component2 cmp2) {
this.cmp2 = cmp2;
}
}
and
#ClientWidget(value = VComponent2.class)
public class Component2 {
}
I would like to bind them on server side.
...
Component2 cmp2 = new Component2();
Component1 cmp1 = new Component1();
cmp1.setDataSource(cmp2);
mainWindow.addComponent(cmp1);
mainWindow.addComponent(cmp2);
...
Question is that I don't know how to send bind infomation to VComponent1.
VComponent1 should have direct link to VComponent2
public class VComponent2 implements Paintable {
public String getCurrentData() {
return "Hello";
}
}
public class VComponent1 implements Paintable,
ClickHandler {
VComponent2 dataSource;
#Override
public void onClick(ClickEvent event) {
super.onClick(event);
String data = dataSource.getCurrentData();
client.updateVariable(uidlId, "curData", data, true);
}
}
I need to avoid communication through server part of Component2 because of some specific time issues.
VComponent1 should have direct access to VComponent2.
Could you please help me with my scenario.
Thanks,
Aritomo
You can communicate a reference to another Vaadin component like this:
Server-side:
public void paintContent(PaintTarget target) throws PaintException {
..
target.addAttribute("mycomponent", component);
..
}
Client-side:
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
..
Paintable componentPaintable = uidl.getPaintableAttribute("mycomponent", client);
..
}

Castle and generics

Considering this code :
interface IRepository<T>
{
void Save();
}
class Repository<T>
{
public virtual void Save() // something
{ }
}
interface IOtherRepository : IRepository<OtherClass>
{
void Other();
}
class OtherRepository : Repository<OtherClass>, IOtherRepository
{
public override void Save() // something different
{ }
public override void Other(){ }
}
How is it possible to configure Castle Windsor to give me an instance of OtherRepository when I call container.Resolve<IRepository<OtherClass>> ?
If Castle Windsor can't do this, which ioc containers can ?
var container = new WindsorContainer();
container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>));
container.Register(Component.For<IRepository<OtherClass>, IOtherRepository>()
.ImplementedBy<OtherRepository>());
var repo = container.Resolve<IRepository<Something>>();
Assert.IsInstanceOfType(typeof(Repository<Something>), repo);
var specificRepo = container.Resolve<IRepository<OtherClass>>();
Assert.IsInstanceOfType(typeof(OtherRepository), specificRepo);
var otherRepo = container.Resolve<IOtherRepository>();
Assert.AreSame(otherRepo, specificRepo);