WinRT Application - c#

I am busy developing a WinRT Application.
I want to access the value of RichEditBox defined in page BasicPage1.xaml into the code behind the page BasicPage2.xaml i.e in BasicPage2.xaml.cs?
Is there anyway to get the value of the RichEditBox(defined in BasicPage1.xaml) in BasicPage2.xaml.cs ?
Thanks in anticipation.

Are you familiar with MVVM? Basically the idea is to not rely to much on the control layer for business data, instead share these information on another layer, in this case the model or view model.
So lets say you want to want to load a project and have a dialog with a textbox containing the path to a project, which the user can modify. So you would store the path in a model called ProjectInformation, this object you can now pass to other views (to be more precise, view models and then views) and use the data there. The important part here is lifetime, your model propably lives much longer than your view, so the data is stored and reused in the places where its necessary.

A simple way to do this is to give your textbox a name in the XAML and then access that textbox via the name in the code behind.
<TextBox Name="myTextBox"/>
then in the code behind you can do this
myTextBox.Text = "blah";
A better way is to use binding so that updating the textbox automatically updates the property you are bound to. Have a look at this post textbox binding example
For a rich edit textbox you should be able to do this:
set
myTextBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, "Here is text");
get
string value = string.Empty;
myTextBox.Document.GetText(Windows.UI.Text.TextGetOptions.AdjustCrlf, out value);
See this post for more information

Do you need to send it through when navigating to the other page? Then you can do it like this:
this.Frame.Navigate(typeof(BasicPage2),textbox.Text);
and at the BasicPage2.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var textbox= e.Parameter;
...
}
But i also highly recommend using MVVM in your application. With MVVMLight you can implement this quite easy and quick.

Related

How to set same viewmodel to two views in fresh mvvm in xamarin forms

I have two views Add and Edit view both views are different UI but same viewmodel. By using mvvm i have set binding context to both view as same viewmodel. I am new to fresh mvvm. In fresh mvvm, I don't know how to set same viewmodel to two views. Another one is, I have passing parameter to the viewmodel constructor, how it is possible in fresh mvvm. Please give your valuable suggestion.
Normally, the accompanying view would be inferred by the names. But for this use case, you should explicitly mention which view to use. There is a method for that, you should be able to use it like this:
// For adding
CoreMethods.PushPageModel<AddEditViewModel, AddView>();
// For editing
CoreMethods.PushPageModel<AddEditViewModel, EditView>();
Of course, taking into account the actual parameters that you still need to specify for the PushPageModel method. Also, I have assumed the names of your view model and views here, but I hope it gets the point across.
For those interested in doing this for the first page you load in your app, such as a Splash screen, this might help -
Page page;
if (Xamarin.Forms.Device.Idiom == TargetIdiom.Tablet)
{
page = FreshPageModelResolver.ResolvePageModel<SplashPageModel>();
}
else
{
// We will load our phone only splash screen
page = new SplashPagePhone();
FreshPageModelResolver.BindingPageModel(null, page, new SplashPageModel(FreshIOC.Container.Resolve<DataService>()));
}
var navContainer = new FreshMvvm.FreshNavigationContainer(page)
{
BarTextColor = Color.White
};
MainPage = navContainer;

Windows 8 questions (Why App.Current.Resources? Why Use GridView Items?)

I have 2 questions regarding a tutorial that I am going through.
Q1.
Through the tutorial they use a datasource
Using the data in the app
To use the data in the app, you create an instance of the data source
as a resource in App.xaml. You name the instance feedDataSource.
BR211380.wedge(en-us,WIN.10).gifTo add a resource to an app
Double-click App.xaml in Solution Explorer. The file opens in the XAML editor.
Add the resource declaration, <local:FeedDataSource x:Key="feedDataSource"/>, to the root ResourceDictionary, after the
MergedDictionaries collection.
and then they use it in the OnLaunch method.
var connectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
if (connectionProfile != null)
{
FeedDataSource feedDataSource = (FeedDataSource)App.Current.Resources["feedDataSource"];
if (feedDataSource != null)
{
if (feedDataSource.Feeds.Count == 0)
{
await feedDataSource.GetFeedsAsync();
}
}
}
I am wondering why do they store it in resource? Wy not just create an instance of the class and get the results from it?
Q2.
Later down the article they use this datasource items with "grid view items". I seen this done in their other template projects. I am wondering is there the standard way of making your interface?
At first I thought maybe just drop some image buttons on the screen and hook up their click events but now I am not sure.
The XAML Resource essentially does create an instance for you and makes it available in the Resources collection, so you could instantiate the class yourself. Having it as a resource keeps this object around and makes it accessible across the various pages in your application. You could certainly create the class explicitly, and if you enforce the singleton pattern on it, it would be semantically equivalent.
I'm not sure I see the context of your second question in the tutorial, but in general the pattern you are seeing is Model-View-ViewModel (MVVM), which is the de facto standard pattern for Windows Store apps. feedDataSource is providing the model and portions of that are assigned to DefaultViewModel, which is the DataContext for all of the binding markup in the XAML pages, which are the views. The idea behind this to separate your data from your model, so that when you do things like load a new data feed, etc., all you need to do is change the data source, and all of the data binding markup will automatically reflect the new data in your user interface.
If you find yourself writing code that looks like TextBox.Text = "My text", then you're deviating from the pattern.

Setting text of controls on form dynamically

I have a WinForms app that I am currently implementing a translation engine in. What I have so far is a bunch of text documents that follow the syntax like:
messages.manualupdate="There is a manual update available for ProgName.\n\nDo you want to update to version {0}.{1}.{2}{3}?"
messages.errorcopy="Clicking OK will copy the error so you can paste it elsewhere!"
messages.error="Error"
messages.notsupported.title="Unsupported client"
messages.notsupported.message="This version is no long supported. Please wait for an update."
I have lots of these for different languages, for example:
messages.manualupdate="é disponibile un'aggiornamento manuale del programma ProgName.\n\nVuoi aggiornare alla versione {0}.{1}.{2}{3}?"
messages.errorcopy="Cliccando OK eseguirete una copia degli errori visualizzati"
messages.error="Error"
messages.notsupported.title="Client non supportato"
messages.notsupported.message="Questa versione non è utilizzabile al momento. attendi il prossimo aggiornamento!"
I then parse this into a DynamicObject which I can access like language.messages.notsupported.error. What I would like to know is if I can somehow link all the controls on the form to use variables from the dynamic object on creation. For instance I have a button on my form that I want to have the text "Error" in. Before the form shows, I set the language variable to the users chosen language, and then when the form shows it simply loads the text from language. Is there a way to do this in the designer rather than having to write a method that is called in the Forms constructor as it seems to me like a little bit of a waste to set all the button text to a value and then change them all when the form loads. I'm looking for a sort of binding, but to the controls Text parameter.
Anyone have any ideas?
MSDN has a walkthrough on string localization that might be of use to you link
Honestly, the approach you are trying to avoid looks best to me. I will suggest you to create a property for the control where you are trying to set the Text. In Set attribute, check for the language selected and get the appropriate text for you.
public string Error
{
set { _errorLabel.Text = value; }
}
private void SetText()
{
if(EnglishSelected)
Error = "English";
}
Regarding waste of time, well, I will just suggest not to set anything in designer and directly set the property in Load form. But I would like to add one more point here that any of the approach will not hit your application speed. First its about making your application expandable and maintainable and then about making it fast. Setting logical things in designer is always a bad practice. If your application is not tiny/small then I will suggest you to follow some design patterns like MVP and move all this logical things in Presenter. Not trying to preach but just suggesting.
And yes, in our company one of team is working in localization part of the application. Using resource may be a better way of doing this.
Hope it helps.

How to save property of Code behind page in ASP.NET 4.0 (not use static)?

Now I have a change to build a web application in asp.net. The style of ASP.net brings me some weird. The hardest problem is that I couldn't save the value of variable after each PostBack event (when we click button). I've see one solution in the question Dynamic User Controls get and maintain values after postbacks but it just only familiar with the value which binding with controls.
Now I think about 2 solutions:
Like the reference question above, I’ll unbind the data when the page PostBack. I’ll save a variable in a Session and in the UnBind method, I’ll reload to variable in session.
Use the ajax Button (not reload all pages): I really want to use this method, but it sounds very easy to be error. I feel very hard to use Ajax control in asp.net.
My code:
public class MainPage
{
//variable
private List<string> lstName;
public MainPage()
{
if (!IsPostBack)
{
lstName = new List<string>();
}
}
}
Now I found a method to save property of Code Behind Page in ASP.NET 4.0.
That's use ViewState["variableName"] variable. When I need to save a property (e.x var ttsHandler), I save it: ViewState["ttsHandler"]=ttsHandler;
When I need to load its value, I have to type casting:
ttsHandler=(TTSHandler) ViewState["ttsHandler"];
But this solution still only useful with well-known Class type (string, int...) because it have to be Serializable. Unfortunately, some property I can't assign its Class Serializable.
Ex: I have to assign a MyThread class (subclass of System.Thread.Threading), and the debugger require project to Serializa System.Thread.Threading class, that's impossible.
Now I have to use another method, that's not so good, is using Session["var"] instead of ViewState. I'll try my best to handle this, and I'm very glad with your help.

Re-using another page section

I have created a web page that I use as a small dashboard to hold issue or no issue. It works great. The page uses an .aspx and .aspx.cs. I would like to be able to reuse the information on this page on other pages. My site already uses master pages and I have not been able to find an easy way to include this information.
How can I use an include from a page that has coding in the code behind easily?
Typically you use Web User Controls for this.
Web User Controls allow you to package up other controls into one that you can drop onto multiple pages. They are great for common UI items such as address entries, dashboards, etc. Basically anything that needs to be the same across multiple pages.
At the risk of seeming very obvious - do you mean usercontrols. These will allow you to reuse chunks of functionality across your site.
I guess this question falls into two categories: User Controls, and Code Reuse. Not sure which one you are after.
User Controls
If you are talking about the controls on your page you will want to create a common user control.
Code Reuse
You need to create a common class (whether it is static or not depends on how you intend to use it) and define functions within that class.
For instance, lets say you have a page that you want to print "Hello World!" on any aspx/.cs page.
You could do this
public static class MyClass
{
public string PrintHelloWorld()
{
return "Hello World!";
}
}
Then you call it from any of your pages like so:
MyClass.PrintHelloWorld();
Right click on the project > Add New Item...
Select User Control (.ascx)
Put your markup & code behind there.
Then you add that control in any other page (includding other controls [although I wouldn't recommend that])
It sounds like you may want to create an ascx User Control.
http://msdn.microsoft.com/en-us/library/ie/2x6sx01c.aspx

Categories