How to avoid constructor to be call twice when a page is define twice in a page - winrt-xaml

I am creating a winRt app. In which I am having a Home.xaml page which having a another page called Market.xaml. So for snapped mode the same code is repeated.
Now my itemListView (used for snapped) and itemGridView (for full view) both have this page (Market)
<views:Market x:Name="viewMarket"/>
And the constructor of this page is called twice which I not want.
So do I use some flag kind of thing or some better approach is there.
Thanks

So, let's talk about this:
XAML is basically a varying syntax to C#. So, when XAML references a control like your views:Market with <Views:Market />, you are actually putting in something like new Views.Market() in both places. Basically, invoking the class twice. Should the constructor not fire twice, the time-space continuum would split in half. Dogs and cats living together, the whole 9 yards.
But, more fundamental here, what is the purpose of the constructor in C#, or in a XAML class? Is to do expensive things that you would not want to repeat? No. The reason for this is because the completion of the constructor is necessary before the UI thread is released and allowed to render the control. The resulting effect is a UI hang.
Moreover, the C# constructor is a synchronous method. It cannot properly invoke or hold asynchronous operations. This means long-running or expensive tasks that should be invoked immediately, should not be invoked in the constructor because it would also require them to be synchronous and UI-blocking. It is because of these last two points I suspect your constructor is being misused.
The solution is in the XAML framework. The XAML Page pipeline includes the constructor (since it is a C# class and they all have it) but it also includes a Loaded event. In many cases, the hard work necessary to fill page controls is in the Loaded handler. This allows the page to render properly, then starts the long-running action that will ultimately and asynchronously update control content.
In WinRT, the Page pipeline also includes an OnNavigatedTo() virtual method in the base that you can override to the same effect. In the override you can include the hard work of talking to a service, deserializing from a file, or whatever you need to make your UI work. Both the Loaded event and the override can be asynchronous, and neither prevent rendering by freezing the constructor.
But, there's another thing to consider since we're in C# and that the rather common pattern called singleton that allows for us to reference a type in two different contexts but without creating a brand new class. This is accomplished by making the class constructor private, but exposing a public property usually called Instance that returns a single, shared instances in some static place.
That might solve your problem already. Then again, none of that is probably what you need. Assuming you already know all that, the quick answer is you can't prevent a constructor because a constructor is necessary to create a new instantiation of any class, including a XAML view. Instead, whatever you are trying to prevent being double might need to be a combination of the discussions above. An offloaded method, and a static reference to prevent duplicate efforts.
Something like this:
public class Market : UserControl
{
public Market()
{
Loaded += Market_Loaded;
}
static bool AlreadyLoaded = false;
void Market_Loaded(object sender, RoutedEventArgs e)
{
if (AlreadyLoaded)
return;
AlreadyLoaded = true;
// TODO: your work
}
}
But that might not do it for you because the static variable is scoped too large. Instead, you can control if it does the big operation with a dependency property you add to your control. With a boolean dependency property set to false, the second control knows not to do something. With it set to true, the first knows to go ahead. And, so on. This prevents all future use of the view or user control in your app from thinking it should not run because of the static property in the above solution. Then again, that might be perfect.
Best of luck!

Related

How to implement two steps who are similar

Is there any way i can implement two stepts like this(see below) in a single function:
When I click on the "Button_name" button
When I click on the "Link_name" name
Is there any syntax so the cucumber won't care what will come after the string and on these two steps i will not need to make two different functions?
Usually i will implement them separately with something like this:
#When("^I look at the \"([^\"]*)\" button$")
public void smt (String smt){ }
Yes, you could implement one step with one method, and pass what was clicked as an argument to that method.
However, this will make your test code more complicated. So please consider whether you should. Having complicated logic in your test code, will make your tests harder to understand and maintain, thus defeating their purpose imho.
If you want to reuse common functionality, the recommendation is to write helper methods that you can call from your step definition.
For example:
#When ("I click on the {string} button")
public void clickButton(String button) {
clickButton(button);
}
#When ("I click on the "Link_name" name")
public void clickLink(String link) {
clickLink(link);
}
and implement clickButton() and clickLink() to click a button or link respectively. (I've used different clickButton() and clickLink() methods in this example, because iirc these would use different types of elements.)
If needed (or you really want to use a switch) you could use a switch statement to get use the right selector based on the button or link name.
Alternatively, you could implement page objects, add all the relevant selectors for the page object there and call the method on the relevant page object that click that particular link/button, delegating the logic to interact with the UI to the page objects and calling this logic from your step definitions.
If are different context one click button and other click link, the best practices is to have two different steps.
One of the bad practices of using BDD is to think about reusing code within steps.
When you have a lot of logic inside a step, any modification can affect many tests.
Should be concerned about code reuse in page objects.

How to correctly call ComponentDialog from another ComponentDialog

I've been battling with this over the last day, can't seem to get it to work correctly. I would share code but I feel it might confuse matters.
I've created a few ComponentDialogs that are needed to be included in other ComponentDialogs including the main bot.js.
ComponentDialogA is being imported into the main bot.js which gets called fine when I do a replaceDialog or beginDialog. But when I try can call ComponentDialogB from ComponentDialogA it just returns to the last point of ComponentDialogA and fails to hit ComponentDialogB.
So my sub questions are as follows:
Do I need a custom state accessor for each ComponentDialog in order
to do what I want to achieve or is that just for storing specific
data related to that dialog as opposed to the actual dialog position
Related to the first do I need a new dialogSet per ComponentDialog to
handle child ComponentDialogs, to use this.dialogs.add instead of
this.addDialog?
Am I just being dimwitted and messed up my code
somewhere?
I'll try and simplify my code and share later.
Many thanks,
First, there is currently a limitation in v4.2.x version of the libraries that prevent ComponentDialogs from accessing dialogs outside of itself in any way. This means that the dialogs inside of a ComponentDialog can only ever begin dialogs that are siblings to them within that ComponentDialog and never access the outside world.
In 4.3.x timeframe it will be possible for a ComponentDialog to begin a dialog "up" its ancestry chain as well. This means that, for your scenario, if both ComponentDialogA and ComponentDialogB were registered in the same parent DialogSet, ComponentDialogA would be able to call beginDialog('ComponentDialogB' ...) and it would find it.
To be clear though, you will still not be able to address individual dialogs within ComponentDialogs from the outside. Meaning, if your ComponentDialogB had a SubDialogX inside of it, something outside, such as ComponentDialogA, cannot start that "inner" dialog explicitly. Make sense?

mvvmcross TabBarController ViewModel Init() not called?

I have implemented a TabBarController much like the Conference tutorial for iOS. Everything is working well now except it appears that the Init methods on Viewmodels for the tabs never get called as it normally would.
Is there something I forgot to do to ensure that Init is called? If I must, I could move this code to the constructor of the ViewModel, but I'd like to avoid this if possible.
EDIT
I guess what I'm really asking here is if I manually instantiate a viewmodel and "create" a view from it via the extension/helper methods, will init get called at some point in the process? If so, at what point can I expect init to get called on the viewmodel?
The Construction-Initialize-ReloadState-Start (C-I-R-S) sequence is called on an MvxViewModel if it is created using the default ViewModel locator - which is what happens when you call ShowViewModel.
If the ViewModels for the sub-tabs are created by calling new on a ViewModel directly - like in the Conference HomeViewModel.cs#L11 - then obviously this same sequence doesn't happen.
Really the difference here is between:
the tab ViewModels which are just INotifyPropertyChanged objects
the page-level MvxViewModels which are also INotifyPropertyChanged objects, but which you further expect to be initialized within navigations.
If you wanted to unify the two concepts then you probably could... but actually I suspect it might be simpler and cleaner to perhaps give the two class types different names, to just put the init code in the constuctor for the tab view models, or perhaps to just call Init on them yourself in the Home constructor.

ViewModel navigation with TabBarPresenter

I have made a HomeViewModel which contains some other ViewModels to be presented in a TabParPresenter. This works fine and the ViewModels associated Views are presented correctly in their corresponding tabs. However on of the ViewModels have a check in the ctor that tells it in when some conditions apply it needs to navigate to SettingsViewModel , which is not a part of the ViewModels contained in HomeViewModel.
The navigation event gets caught by the TabBarPresenterHost, which is simply the Presenter of the application and if a TabBarPresenter is present and not null it is passed to the TabBarPresenter.ShowView() method. All this happens before the TabBarPresenter is done loading and SelectedViewController is set in ViewDidLoad. Hence the TabBarPresenter relies on using the SelectedViewController to push new Views I obviously get a Null Reference Exception.
In short what I want is:
if (conditionForShowingHome == true)
GoToHome();
else
GoToSettings();
Then when inside SettingsViewModel I have set the stuff I need when going back you return to HomeViewModel.
What breaks this is that the ViewModels are loaded before the View is done loading and the navigation event is executed before the View is done loading. Any ideas how to go around this?
I'm afraid that putting this code inside a ViewModel constructor is likely to lead to problems.
The ViewModel constructors are called during navigations - and not all platforms will support starting new navigations while existing ones are still in progress.
To workaround this, I personally opt to put this type of behaviour in the code before the navigation.
In the case of a normal navigation, this can go inside an MvxCommand execution handler.
In the case of application startup, I do this in a custom app start - see some notes in https://speakerdeck.com/cirrious/appstart-in-mvvmcross

Assigning individual View Models to Nested Views?

I have a MainView which has an associated MainViewModel in my WPF application, assigned via its contructor.
App > start up uri > MainWindow.xaml
public MainWindow()
{
InitializeComponent();
var viewModel = new MainViewModel();
DataContext = viewModel;
}
My MainView holds as many as four nested views or child views who are hidden and displayed based upon the button that has been clicked on the MainView. So we toggling the visibility property vi a binding which gets updated via command bindings assigned to each button on the MainView.
Each nested View does not have an associated ViewModel, all bindings found on child views find their information in the MainViewModel. So binding system ends waling up the UI tree of the app to find out that the parent 'MainView' has an associated ViewModel.
So overall there is 'ONE' -> ViewModel. This seems to work fine and but as expected this VM has gotten to big and needs re-factoring. It holds information that contextually it should not. But this is a proof concept application. So i decided to keep it simple and make sure it was do-able.
PROBLEM:
When i tried assigning a empty view with an empty view model I noticed binding errors in the output window and as expected weird and broken behaviour. Which makes no sense ... Is there a more clear and concise way of letting WPF know how to handle any bindings it finds in a nested view control? I thought if each view's constructor assigned itself a corresponding VM as shown above, then it should work as this logically makes sense. Unfortunately all buttons on the MainView stop working when the corresponding view it is designated to switch on and hide the others has an associated ViewModel. On some buttons it works and the others it does not? This is really weird?
As mentioned in my answer above, the problem was that WPF binding system was struggling to to resolve bindings at run time. The main view has its associated view model instantiated and assigned via the Main View contructor and this pattern is repeated for all nested views that the MainView also houses.
By default, I tend to use the implied binding syntax which means that without explicitly specifiying a source the binding system will try to resolve the name you supply in the binding. So it's all implied and nothing is explicitly set!
Upgrading each nested view to have its own view model makes this auto discovery/resolution of bindings go a little crazy and I have not explicitly told the binding system where to find the property I am looking for hence the output window binding errors.
This leads to unexpected behaviour as the output window was telling that it was trying to resolve binding expressions in nested views --> viewmodels. When in actual fact that VM it is looking in, IS EMPTY!
So clearly the binding system is good when you do not explicitly set a source property inside the binding syntax. It is clever enough to find things on its own. In my case it needed some extra help, as it was unsure as to where to find things.
SOLUTION:
Remove the constructor declaration for the MainViewModel in the MainView constructor.
Scope in an xmlns for ViewModels namesapce into your MainView.xaml
Create a window resource inside the MainView .xaml
Give the resource a key.
Upgrade all your bindings in the MainView xaml file to include source property.
Give the source property a static resource binding that points to your ViewModel key value set up in step 4.
Only do step 6 for bindings who refer to the ViewModel that is associate with the MainView.
All nested views are to be left alone, they should handle their own bindingds in their own xaml files. The MainView simply instantiates them and places them onto the UI. For me this was the case, I did not have any more bindings concerning my nested views. All Bindings that lived on the MainView.xaml file referred to data in the MainViewModel.cs. This makes alot easier when viewing your problem.
For some reason the editor was being awkward so I chose to omit any sample code but the steps above are descriptive enough to follow the steps that I took. Above is what worked for me.
ANOTHER WAY OF SUMMING UP THIS PROBLEM
Most books teach the shorter binding syntax
What happens when more than one data context is available?
How is the binding system supposed to know how to resolve your short hand binding expressions.

Resources