Hi i am using Modern UI and Its loading ".xaml" file by ContentSource and link parameter. So i am stuck with the .xaml Class Object. How could we get the Class Object reference by Content Source or say URI.
My Main class file code is as below
public partial class MainWindow : ModernWindow
{
public MainWindow()
{
InitializeComponent();
this.ContentSource = new Uri("Pages/Home.xaml", UriKind.Relative);
}
}
i want Home.xaml object as Home Class Object. i tried to create a new Home Object as
Home homapge = new Home();
But its creating a new Home class Object not that one, which is used in Cotent Source.
So please help me to get this object.
Related
So I have this page in an UWP Windows 10 app using C#:
public sealed partial class MainPage : Page
This page contains a TextBlock called tbPageTitle.
I'd like to change the text of tbPageTitle to "bla" from another page, so I use the following code:
MainPage.tbPageTitle.text = "bla";
However, I get the following error:
CS0120 An object reference is required for the non-static field, method, or property 'MainPage.tbPageTitle'
I don't know what to do here. I feel like I've read every single Google result.
I found some results to create a new instance of a class, so that would be for example:
MainPage mp = new MainPage();
mp.tbPageTitle.text = "bla";
But wouldn't that create a completely new MainPage? This also doesn't work by the way...
According to the answer to Sandy's comment you have an inner Frame element in the MainPage where you load other pages. So the easiest way to get the current MainPage instance is the following:
MainPage mainPage = (Window.Current.Content as Frame).Content as MainPage;
Note that this will obviously fail if you every navigate outside of the MainPage and call this line there. Additionally note that objects you are creating in XAML are not public, what means that you can't access your tbPageTitle element here anyway, but will need to create any kind of wrapper property in your MainPage like this:
public string PageTitle {
get { return tbPageTitle.Text; }
set { tbPageTitle.Text = value; }
}
However as mentioned by HeySatan, this is not the most beautiful code design you are creating here. Maybe you could create a method to go to a specific frame, something like that:
public enum TabContent { Home, Replies, Messages, Settings }
public void OpenTab(TabContent content) {
// Set Page title and navigate
switch (content) {
case TabContent.Home:
tbPageTitle.Text = "Home";
InnerFrame.Navigate(typeof(HomePage));
break;
case TabContent.Replies:
tbPageTitle.Text = "Replies";
InnerFrame.Navigate(typeof(RepliesPage));
break;
case TabContent.Messages:
tbPageTitle.Text = "Messages";
InnerFrame.Navigate(typeof(MessagesPage));
break;
case TabContent.Settings:
tbPageTitle.Text = "Settings";
InnerFrame.Navigate(typeof(SettingsPage));
break;
}
}
Main goal of this method is that if you have a button Settings in your HomePage you are only calling the following line and all logic to do the navigation stays in the MainPage and HomePage only has logic related to itself:
// In HomePage:
MainPage mainPage = (Window.Current.Content as Frame).Content as MainPage;
mainPage.OpenTab(TabContent.Settings);
If you don't want to access Window.Current.Content all the time, you could also declare a static method in your MainPage class and make access simpler:
// In MainPage:
public static MainPage Instance {
// This will return null when your current page is not a MainPage instance!
get { return (Window.Current.Content as Frame).Content as MainPage; }
}
// Now in HomePage it's only:
MainPage.Instance.OpenTab(TabContent.Settings);
In my MainPage.xaml I've got a SplitView that loads many pages inside a frame created in it's SlplitView.Content.
I've got data in a MainPage's variable that needs to be sent to every page that loads in my SplitView content's frame according to the ListBoxItem clicked.
Also in the current page I may have to update the MainPage's variable before a new page is loaded.
How can I do this? Is there a way to declare a global variable? Can I transport that information from a page to another updating it's value on the parent page?
I think you can declare a public static variable in App Class in App.xaml.cs, and use it in any pages in the app.
In App.xaml.cs:
sealed partial class App : Application
{
...
public static string MyTestVar { get; set; }
...
}
In MainPage.xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
App.MyTestVar = "world";
}
}
And for some other cases like implementing a setting page, you can check Store and retrieve settings and other app data.
Complementing the last answer I solved my problem by declaring an internal static variable in my "App.xaml.cs".
internal static string foo = "";
Then to access it I used:
App.foo = "my string";
There is also a more elegant way to preserve and restore page data when leaving it (which is what I needed) as follows: https://msdn.microsoft.com/pt-br/library/windows/apps/ff967548%28v=vs.105%29.aspx
I Have following Viewmodels in my WPF application.
CustomerViewModel
OrderViewModel
The application is implemented as a multi tabbed interface using Conductor.Collection.OneActive method.
The customer record is consist of an order record.
I need to open a new order screen from customer screen by clicking a context menu.
Simply I need to open a new order window from customer window.
I have initialized an instance of OrderViewModel inside CustomerViewModel and activated it.
But the new screen is opened but I cannot see any data on it.
How can I transfer data from CustomerViewModel to CustomerOrderViewModel while creating a new window in a better way.
you can use INavigationService for this
public class SecondPageViewModel: ViewModelBase
{
public string Title { get; set; }
public SecondPageViewModel(INavigationService navigationService) : base(navigationService)
{
Title = "Second Page";
}
}
public void GoToSecondPage()
{
navigationService.UriFor<SecondPageViewModel>().WithParam(l=>l.Title, "Navigated from MainViewModel").Navigate();
}
you can find sample code links hear :
Navigation with parameters
page navigation and passing complex state
I am looking for some advice regarding the design of the below scenario:
High level info:
I have a WPF GUI that contains a grid listing some cars info.
It contains a button, "Add new Car..." that pops-up a form with some base fields so that the user can add a new ICar object in the list.
Both main window and form are designed following up the MVVM pattern (and some decoupling of the commands as well).
Flow and question:
The button "Add new Car..." is bound to a main window command that loads up the form.
The form is bound to a background object, so that when the user presses "Ok", I would want the object to be returned to the original window.
However I don't know how to design that last step, i.e. should I:
Have some public methods in my form to be called so that it is by itself:
Loading the form (ShowDialog..).
Does the properties bindings (already done)
Returns the new Car object to the caller (here the main window)?
Or:
Call the form.ShowDialog() from the main window.
Do something else (which I can't find how to) to get back the new Car object defined by the user?
Thank you!
Something like this:
interface IPresentationService
{
bool ShowInDialog(ViewModel viewModel);
}
class CarViewModel : ViewModel {}
class MainViewModel : ViewModel
{
[Import]
private IPresentationService presentationService;
private void AddNewCar()
{
var car = new CarViewModel();
if (presentationService.ShowInDialog(car))
{
Cars.Add(car);
}
}
public MainViewModel()
{
Cars = new ObservableCollection<CarViewModel>();
AddNewCarCommand = new RelayCommand(AddNewCar);
}
public ObservableCollection<CarViewModel> Cars { get; private set; }
public ICommand AddNewCarCommand { get; private set; }
}
Where IPresentationService is a service, which is intended to create and show a popup window. Instance of IPresentationService could be obtained via service location or dependency injection ([Import] means dependency injection using MEF).
As the MainView has a button New Car, the Main ViewModel might have a property NewCar.
You could create a new instance of a car when the button is clicked and pass the instance to the dialog that allows the user to enter values for the new car.
When the dialog is closed, the main view could do any additional validation (unique licence plate) against the collection of cars and when found correct, add the new car to the cars collection and possibly propagate this addition/change to the model.
This way the dialog is responsible for the details of the car and the main view is responsible for adding the car to the collection.
How do I open an new window from AccessoryButtonTapped button from my BasisViewController. Right now I have this for open, but the problem is the NavigationController which I can't find because I'm inherent the UITableViewSource.
Opskrift ops = new Opskrift(item.ImageName, item.Name, item.optionTxt, item.SubHeading)
this.NavigationController.PushViewController(this.opskrift, true);
If I use ops.NavigationController.PushViewController(this.opskrift, true);
I get an Object reference not set to an instance of an object exception.
Give your UITableViewSource inherited class access to your controller by passing it through its constructor:
public class MyTableSource : UITableViewSource
{
private BasisViewController controller;
public MyTableSource(BasisViewController parentController)
{
this.controller = parentController;
}
//use like this in a method:
//this.controller.NavigationController.PushViewController(opskrift, true);
}
Your Opfskrift controller's NavigationController property returns null because it is not part of a navigation controller's stack when you initialize it (=hasn't been "pushed" in a navigation controller). Of course, BasisViewController must also belong to a navigation controller for its NavigationController property to contain something other than null.