Get Page instance externally in WinRT - c#

I am currently developing a Windows 8 App which contains 2 pages(page1 , page2).
After navigating form page1 to page2,
using this.Frame.Navigate(typeof(AnotherPage));
how can i access the instance of page2 form page1,(We can implement it by having a static instance property and set it when the constructor is called )
But is there any in-build feature to access the current instance of the page from outside of the current page, Or is this pattern of programming is recommended in WinRT paradigms, is there any way to implement my scenario.

First thing is - by default the instance of the previous page is not kept in memory after you navigate to the next page by default - not unless you change the default NavigationCacheMode of the page, which might not be a good idea unless the page takes a lot of time to load and is like a hub page that you go back to a lot.
The approach I would suggest is to use the MVVM pattern, so pages never have to talk to each other and it's the view models that would do it. Then use a pub/sub pattern with a helper like the Messenger class in MVVM Light Toolkit to send weak events/messages instead of adding tight coupling between objects.
All in all - it depends on what you want to do and why you want your pages to talk to each other. You could pass a parameter in the Navigate() call (make sure it's just a primitive type though if you want to support app suspensions using the SuspensionManager class and built-in serialization from the Frame class). You could also use some sort of a global repository or settings service to share data between pages.
If you're not willing to invest in these things - using the plain old static class might be enough for you. Whatever works.

Related

ASP.Net Razor C# Variables Accessible to All Pages

For starters, please forgive me and please correct me on my terminology. I am quite sure of the correct words to use for what I am trying to accomplish.
I have been given the task of building an ASP.Net Razor web site. It is something new to me. I am very proficient in PHP and ASP Classic. What I need to be able to figure out is how to declare a variable that is accessible everywhere. I do not know if in the .net world you call it a global variable or application variable or something else. But, here is how I would do this in Classic ASP.
In Classic ASP, I would have a file named VarFunct.asp. It would be the file that I declare my variables and have various functions I would access from anywhere. I would include the VarFunct.asp file on all of my pages. Anyway this is what I am really trying to do (written in how I would do it in Classic ASP)…
SelLoc = Request("SelLoc")
If Len(Trim(SelLoc)) = 0 Then
SelLoc = "All"
End If
In this case, Request("SelLoc") could be Request.QueryString or Request.Form. Then anywhere in my website I could use the variable SelLoc. So, in short... I need to be able to set a variable. Check to see if it is set by Request.Form, if not, check Request.QueryString, if not set the value to “All”. How do I write this? And where do I put it?
When I created the website using Visual Studio 2012, I selected ASP.NET Web Site (Razor V2).
This seems like it should be such a basic fundamental task of any website that has any kind of server side programming, but trying to find information and documentation online is near impossible, but probably because I am not using the correct terms for my question. I have not found any Razor tutorials that talk about setting variables that can be used globally across the website.
If someone could please help me out here by either telling me what I need to do or point me to a good tutorial, that would be great.
what you are looking for is called Static Class/Member
This will allow you to store and share data for the whole application.
BUT! since web server is multi-threaded, you wouldn't want to do this or else you might run into the case where the data is overwritten by another request before you finished the current one.
If you need to pass data from controller to your View you can use ViewBag dynamic object
If you need to use the data anywhere else (for example in a helper class) then do
HttpContext.Current.Application["VariableName"] = something
It is basically a dictionary and each request will have a different Application object
There are several ways of doing this.
For your example I would assume that this particular variable can be different for different users that are using the application at the same time. This is more of a Session scope than Application scope.
In this case you could simply use inheritance and make a base controller and/or base view model class that all your other controllers and/or view models inherit from. This way you can pass it back and forth between the view and controller and read/update it whenever you need to.
You could also use the Request and HttpContext and Session objects that are built into asp.net, if they fit your need. A brief overview of some of their functionality can be found here: https://learn.microsoft.com/en-us/aspnet/web-pages/overview/api-reference/asp-net-web-pages-api-reference --- google searching specific ones yields tons of results.
If you truly want Application scope, you can of course use a static class for you utilize static methods. Then you don't need to include the class in every other class, but you would need to fully name qualify the method when you call it.
I would not recommend static variables at this level though. I can't imagine very many things that would need to change for every single user that you would change while the application instance is running. Most of these sorts of items that we use are caches (generally db lookups and such, that we don't want to retrieve from the db each time, and RARELY get updated). If you utilize caches, be very aware of your thread safety when updating them. Here is an msdn on caching: https://msdn.microsoft.com/en-us/library/aa478965.aspx --- Or application configuration settings, like the application environment. We pull most of those from a config file, and they are read only, we don't change them within a running instance of the application.

Alternatives on passing parameters from ViewModel to ViewModel in MvvmCross

We are at the preparation on porting a huge windows mobile app to Xamarin
and we are using MvvmCross to help us with the Mvvm.
The application is huge, where workflows live between several pages. So there is a need to pass states/objects between pages. As those states can be big, it does not make sense to serialise them between navigation calls.
My question is: what are any proven or used alternatives to pass objects between view models? Is there some session manager?
Note: we are starting with Android, so maybe there is also good an Android only solution.
Hint: I posted this question on Programmers as well, not sure what's the better platform for this: https://softwareengineering.stackexchange.com/questions/285219/alternatives-on-passing-parameters-from-viewmodel-to-viewmodel-in-mvvmcross
A couple of options I use are:
1) Persist state to a SQLite database and pass an identifier from ViewModel to ViewModel. This is simple and ensures that the state remains even between app restarts.
2) Another option, which is useful in a wizard-like setting is to use a Cache service. I simply created an interface to add and remove entries in a cache by key. I treat it like a standard MvvmCross service and use IoC to inject into my view models. At the start of the process, create a GUID to use as your key, add your state to the cache. Simply pass the key to the next view model, where it can retrieve the state from cache.

Design Issue (Unity, Reflection)

I am currently developing a Microsoft Word Add-In that communicates with a backend via webservices. The dialogs in this Add-In are created with WPF and I make use of the MVVM pattern. The viewmodels communicate with the repository over services. In order to decouple viewmodel and services I use the DI-container framework Unity.
There is some kind of state (I call it "Context", similar to the http-context) that depends on the active document at the time a window/viewModel was created. This context contains stuff like the active user.
Since a picture is worth more than a thousand words, I prepared a diagram to illustrate the design.
Now my problem is, that when a service method is called, the service needs to know what the active context is in order to process the request.
At the moment I am avoiding this problem by having one service for each document. But since this cuts across the statelessness of services, I don't consider it as a durable solution.
I also considered passing the context to the viewModel, so that I can pass it back to the service when calling a method there. But I see three problems here:
Technical Problems:
Each time I want to resolve a Window with Unity I would have to pass a ParameterOverride-object with the context - what creates dependencies to the concrete viewModel implementation.
=> Or is there a better way to achieve this with unity?
Cosmetical Problems:
I would have to pass the Context as object since the class for it is part of the startup-project and the ViewModels are not. When I now want to obtain data from the context, I'd have to cast it.
Consider a windowViewModel that contains data for a TreeView with hundreds of TreeViewItems. I would have to pass the Context to each of these "TreeItemViewModels" if I'd want to call a service-method in one of these.
So I'm wondering if there is a way of automatically "injecting" (maybe reflection?) the context into the viewModel at runtime without the viewModel knowing anything about it. This is probably impossible to achieve with Unity, but I'm always open to being convinced.
On the other side, when a method on a service is called, the injected context is automatically extracted (maybe by some kind of layer in front the actual service) and saved into some globally accessible property.
I hope that some of you can help me. I appreciate any kind of idea.

Reuse IMobileBarcodeScanner in cross platform app?

I am writing an app using xamarin, mvvmcross, and zxing.net.mobile. I would like to be able to use an instance of IMobileBarcodeScanner in the portable class library to do the scanning.
The issue I'm running into is that the droid version of IMobileBarcodeScanner requires a context to be passed into the constructor. I'm guessing we'd need to register the type in the view constructor so we can pass the correct context.
Since there will be multiple views, would I be able to register a different instance of IMobileBarcodeScanner for each view? Or, could I make the IMobileBarcodeScanner a public property on the view model and set it from the view constructor instead?
Thanks for your help!
There are two main ways I've worked with barcode scanners in the past.
I've launched them as 'new pages' - including using external activities and StartActivityForResult in Android
I've treated them as 'normal controls' - a bit like TextEdit fields within the current page.
When doing the first of these, I generally used a pattern similar to the PictureChooser plugin - on Android, this accesses the current context using IoC in MvxAndroidTask
When doing the second, I treat this purely as a View concern - all the ViewModel needs to provide is an ICommand and/or a string property which can be bound to the scanned event or to the scanned text.

Register User Interface Elements in Engine / Controller

I am currently writing a little game with standard UI controls.
It is working great so far but what I don't like is that I am managing the controls in the codebehind file. (C#)
As I am trying to decouple all elements, I would like to have a separate engine / controller which handles all the data management and the logic for my user interface.
Is there a possibility to register the controls with the engine so that I don't need to pass them down with every method I call?
Currently I am forced to pass the controls every time I call the function..
Thanks in advance!
Will be good if you can elaborate more on your current implementation. From what I can understand, instead of trying to figure out how to 'register the controls with the engine', why not try to see if it's a design issue.
Perhaps there's a better way to structure your app/classes/components so that you can decouple logically for better reusability and maintainability?

Categories