Quick question.. I'm trying to access a UI control, on a specific Page that is loaded on startup, by using JavaScript.
My MainPage uses a Frame to show a specific Page. From App.xaml.cs I'd like to access an UI control on this specific Page.
Accessing the MainPage is do-able:
MainPage m = (MainPage)Application.Current.RootVisual;
m.testField.Text = "Working!";
But how can I access the Page that is loaded into the Frame on the MainPage.xaml.cs?
Kind regards,
Niels
Hope this helps..
MainPage page = Application.Current.RootVisual as MainPage;
LoadedPage myPage = page.frame.Content as LoadedPage;
Related
I have some controls on a page in a Xamarin.Forms app that DO NOT WORK when navigating back to the page. I can only "go back" to the page by navigating to the page outright (I'm using Prism to do this) and it works.
When transitioning to MAUI, I'm writing my own navigation service to replace Prism. Everything works, except navigation to replace the first page and clear the navigation stack. This is NOT App.MainPage.Navigation.PopToRootAsync() The page is the first page in the navigation stack. Attempting to replace App.MainPage with a new page does not work. Nothing happens if I do this.
Example navigation stack:
MainPage
Page1
Page2
Navigation stack after navigating to new root page:
MainPage (but new instance of page, not the original instance). Remainder of navigation stack has been cleared.
I'm not sure what I was doing wrong, but further testing shows that you can replace MainPage for Maui.
You CANNOT just assign a NavigationPage to MainPage. This will work for the navigation and any logic in associated viewmodel will work, but the UI will be blank.
If you just assign a ContentPage to MainPage, everything works great.
Is it just me, or is it impossible to navigate to a page that does not have a .xaml head?
I am constructing a page entirely in code, and I want to navigate to it. I do not want a xaml page because this is a class library and also it is constructed based on data received. I know all about using .xaml to create the page with templates, binding, etc., but I want to avoid that.
When I call Frame.Navigate(typeof(CodePage)), I get nice AccessViolationException.
My page is simple, and so is the navigation. This is the code with a clean, new project
Navigation (button click):
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Navigate(typeof(CodePage));
Page:
public class CodePage : Page
{
public CodePage()
{
Content = new TextBlock
{
Text = "It works!",
};
}
I know about this issue: Navigate to a Page of another Class Library but, this is because ALL the pages are in the library, I just have 1 specific page in my library. Also, I have other pages in the "launcher" app.
Navigation only works with pages who have xaml part as well because when the page does InitializeComponent in its constructor it sets up the page for Navigation routes and NavigationCache etc.
I read around hundreds of Q/A & Blogs for the same, but not able to resolve the error I am getting. In my WPF application I need to navigate from a MainWindow.xaml to a Page Register.xaml. I have below code :
Register register = new Register();
MainWindow.Navigate(register);
Or
this.NavigationService.Navigate(new Uri("Register.xaml ", UriKind.Relative));
It is giving me error
'.MainWindow' does not contain a definition for 'Navigate'
Or
MainWindow' does not contain a definition for 'NavigationService'
To open a page in you need to have it in a frame.
A page can be hosted from Window, NavigationWindow, Frame, or from a browser. To be hosted, a page can be:
The direct child of a Window, NavigationWindow, or Frame element in
XAML.
Instantiated and set as the value of the Content property of Window, NavigationWindow, and Frame.
Set as the uniform resource identifier (URI) source of the Source property of either NavigationWindow or Frame.
If you want to have it in your MainWindow you can do the following.
XAML
<Frame Name="contentFrame" />
C# code-behind
contentFrame.Content = new Page();
To open it in a new window you could do something like this.
Page p = new Page();
p.Show();
Is it possible to rebind a repeater (inside master page) from content page?
My pages are base on master and content pages and I have some links (anchors) for download files!
After click on those links page_load of content page fires, and will show download window. But we will never get page_Load of master page and in master page I have a summary for showing download counts.
How can I rebind that summary (inside master page) from content page before showing download window?
Yes, it's pretty simple. You can 'find' the control from the content page.
Here's a sample where i'm binding to a GridView control.
Master:
<asp:GridView runat="server" ID="gridViewMaster" AutoGenerateColumns="true" />
Content Page:
var gridView = (GridView) Master.FindControl("gridViewMaster");
gridView.DataSource = dt;
gridView.DataBind();
Just replace the grid view object and control id with your repeater...and bind it to whatever object you want.
Edit - Here's the code to find a server side div:
var divMaster = (System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divMaster");
divMaster.InnerHtml = "<h2>Hello World</h2>";
Yes. You have access to the master page from your web form—using this.Master—which you'd then have to cast as the appropriate type. From there you can access any public methods or properties you've defined.
Simple add a ReBind method there that does what you want, and you should be good to go.
EDIT
It would be something like:
(this.Master as WhateverTypeYourRealMasterPageIs).ReBind();
You can expose the master page control to the children. Make the control public with an accessor in the master page... such as:
public Repeater MasterpageRptr {get;set;}
Then on your child page, add the MasterType definition:
<%# Page Language="C#" Title="Content Page" MasterPageFile="~/MyMaster.master"%>
<%# MasterType TypeName="MyMaster" %>
(where MyMaster is the master page class file)
Then you can call this in your child code-behide using the accessor.
.
.
Please vote if helpful.
The proper way to do so is for the user control to fire an event that the Master page subscribes to. Let me know if you need some sample code
Some sample code:
In your user control, add the following event:
public event EventHandler RefreshRequested;
The user control will throw this event whenever it wants a refresh by calling the following method:
private void OnRefreshRequested()
{
//make sure the event is being listened to. no point raising an event if no one cares!
if (this.RefreshRequested != null)
{
this.RefreshRequested(this, EventArgs.Empty);
}
}
Now, the master page will subscribe to the user control's event and acts accordingly. Subscribing to the event is just like subscribing to any other event (ie: Button_Click).
Let me know if this clears things up.
Is it possible to acces a control thats located in a content page (withing a content place holder, a multiview control to be more exactp) from the master page?
The situation is, i have a menu with buttons thats located in the master page.
Now in my content page i have 1 content place holder.
In which a multiview with several views is located.
If i press a button in the menu (MasterPage) then it should open the proper view (with its controls) displayed in the content place holder area.
I have set the ActieveViewIndex=0 but i am getting all sorts of wierd behavour.
I have to do something with the ActiveViewIndex++ somewhere but nothing seems to work.
edit::
string a = Request.Querystring["one"]
string b = Request.QueryString["two"]
if ( a == "addOne") // where addone is a redirection to the content page from the master page button
{
mvMultieView.SetActiveView(vView1);
}
else
if ( b == "addTwo")
{
mvMultieView.SetActiveView(vView2);
}
Any suggestions?
Kind Regards.
You can easily do that using find control
View myView = (View)this.Master.FindControl("PlaceHolderFullMain").FindControl("PlaceHolderMain").FindControl("Mymultiview")
The way my team and I accomplished this task (and I don't know that it's the best method, but it was effective) was to use query strings (as you had in your previous question it looks like). We established a standard QS variable called iView that would determine the name of the view in question (not necessarily the control name itself, but some keyword that the content control would respond to). Since all of our pages/controls have a page base class they inherit from, we put a method in the base class (in our case it was at the page level, but in yours a control level might work) that was responsible for getting the requested view. In the control we would have a mechanism (switch perhaps) that would set the activeView. In some cases we just used the actual ID of the control (since it was a mystery to be obfuscated) and avoided the switch altogether.
http://www.mydomain.com/mypage.com?iView=mySecondView
partial class MyControl : System.Web.UI.WebControl
{
// blah blah control stuff
public string getRequestedView()
{
return (Request.QueryString["iView"]) ? Request.QueryString["iView"] : String.Empty;
}
}
...
protected void Page_Load(object sender, EventArgs e)
{
View myView = myMultiView.FindControl(this.getRequestedView());
if(myView != null)
this.MyView.SetActiveView(myView);
}
have you done this change in the Master page or content page?
because i also got the same problem.
I have link buttons in my master page which i need to activate view controls in content page.
the content page is not the currently loaded page.
will this method help for my solution?