I have a ResourceDictionary class as follows:
using System.Windows.Navigation;
using static myApp.filesXAML.Login;
namespace myApp.Clases
{
partial class functionsMenu : ResourceDictionary
{
private void imageCloseMyApp(object sender, MouseButtonEventArgs e)
{
NavigationService.Navigate(new Uri("filesXAML/Login.xaml", UriKind.Relative));
}
}
}
The imageCloseMyApp function is invoked from an image by clicking on it and I wish to call another page.
And I get the following error before compiling the project:
Severity Code Description Project File Line Status deleted Error
CS0120 An object reference is required for the field, method or
property 'NavigationService.Navigate (Uri)' not static myApp
H:\pro\Visual_Studio\myApp\myApp\Classes\ FunctionsMenu.cs 35 Active
I have searched the internet, and tried the following options:
Login page = new Login();
page.NavigationService.Navigate(new Uri("filesXAML/Login.xaml",UriKind.RelativeOrAbsolute));
// or
NavigationService nav = NavigationService.GetNavigationService(Application.Current.Windows[0].Parent);
nav.Navigate(new Uri("filesXAML/Login.xaml", UriKind.Relative));
But none works.
Any suggestions or comments?
I have achieved it as follows:
NavigationService nav = NavigationService.GetNavigationService((Grid)((Image)sender).Parent);
nav.Navigate(new Uri("filesXAML/Login.xaml", UriKind.Relative));
I guide myself to understand the following thread:
How do you get the parent control from an event send by a control in the resources
Related
Ok so in my previous design, on my MainPage which is called TasksGroupPage, i had a "+" icon that was taking me to a NewTasksGroupPage to fill a form and then click the save command to create a new TasksGroup ( so i don't need the + icon function to load the page anymore).
This was working perfectly and after clicking the save button it would take me to the MainPage.
But my problem is that i changed my design, i now removed the + icon and instead i created a new tab which now takes me to the NewTasksGroupPage.
To create this tab i simply added to my TasksGroupPage( the main page) the following code : <views:NewTasksGroupPage></views:NewTasksGroupPage>
The problem now is that :
When i click the save command in my NewTaskPageViewModel , the page doesn't take me to the last page like it did before ALSO it's leaving all the text in the entry box there and it only create my TasksGroupPage after a while.
How should i set up this new design to work properly ? If impossible to do, is there a way to add a GestureRecognizers to one of the tab ?
Thanks.
Here you can see my code right now :
TasksGroupPage.xaml
<TabbedPage>
<ContentPage>
// some xaml
</contentPage>
<views:NewTasksGroupPage></views:NewTasksGroupPage>
</TabbedPage>
TasksGroupPage.xaml.cs this is the function that i don't need anymore but this is what i was using with the + icon
protected async void GoToNewTaskPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new Views.NewTasksGroupPage());
}
NewTaskPageViewModel.cs this is the end of my save command which before took me to the main page ( TasksGroupPage) but now it just stays there and doesn't reset my entries to blank ( even when switching between other tabbed pages)
async Task SaveNewTask()
{
//some code
await Application.Current.MainPage.DisplayAlert("Save", "La tâche a été enregistrée", "OK");
await Application.Current.MainPage.Navigation.PopAsync();
NotifyPropertyChanged();
}
If you read the document Xamarin.Forms Navigation:
You will find that NavigationPage and TabbedPage are two different ways to navigate pages in Xamarin.forms.
Navigation.PushAsync and Navigation.PopAsync only works in NavigationPage, it does not work in TabbedPage, that's why the page doesn't take me to the last page like it did before.
If you use a TabbedPage, you can use below code to switch page:
async void SaveNewTask(object sender, EventArgs args)
{
//this here means the current ContentPage
//this.Parent = NavigationPage
var tabbedPage = this.Parent.Parent as TabbedPage;
//if you don't have navigationPage, just use:
//var tabbedPage = this.Parent as TabbedPage;
//Switch to first page in tabs
tabbedPage.CurrentPage = tabbedPage.Children[0];
}
I made a small UWP app which has a Settings page for its NavigationView. In it, I made a Slider which allows the user to change the Acrylic opacity (TintOpacity) of the Background of the Page (or basically NavigationView.Background). But I have an error
CS0120: An object reference is required for the non-static field, method or property 'MainPage.ChangeAcrylicOpacity(double)'
<!-- MainPage.xaml -->
<NavigationView
x:Name="NavView"
ItemInvoked="NavView_ItemInvoked"
Loaded="NavView_Loaded"> <!-- and some other attributes -->
// MainPage.xaml.cs
public void ChangeAcrylicOpacity(double tintOpacity)
{
AcrylicBrush acrylicBrush = NavView.Background as AcrylicBrush;
acrylicBrush.TintOpacity = tintOpacity;
}
//-----------------------------------------------------------------------------------------------------------------
// Settings.xaml.cs
private void Slider_AcrylicValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
Slider slider = sender as Slider;
double tintOpacity = slider.Value;
MainPage.ChangeAcrylicOpacity(tintOpacity);
}
The error is in Settings.xaml.cs > Slider_AcrylicValueChanged > MainPage.ChangeAcrylicOpacity(tintOpacity);
And this error goes when I change MainPage.xaml.cs > ChangeAcrylicOpacity(double tintOpacity) from public to private, but gives an error 'MainPage.ChangeAcrylicOpacity(double)' is inaccessible due to its protection level
Please suggest some ways to use one page's functions on a different page without errors.
Note: I already searched the internet but no site solved my problem (including StackOverflow).
Try defining "MainPage" property in the Settings class so that you can access the MainPage through it. By the way, access-modifier for the ChangeAcrylicOpacity() should be either "public" or "internal" in that case.
// Settings.xaml.cs
private MainPage MainPage
{
get { return (Window.Current.Content as Frame)?.Content as MainPage; }
}
private void Slider_AcrylicValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
Slider slider = sender as Slider;
double tintOpacity = slider.Value;
MainPage.ChangeAcrylicOpacity(tintOpacity);
}
I'm trying to add a Link to a webform page from a user control. The CSS file contains the styling used by the control. When I try, I get the following exception:
The control collection cannot be modified during DataBind, Init, Load,
PreRender or Unload phase
So, when can anything be added to the controls collection? Seems to me the exception message suggests that all options are off the table. Here's the code:
public partial class AddNoteDlg : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterFiles();
}
private void RegisterFiles()
{
Page.ClientScript.RegisterClientScriptInclude(GetType(), "addNoteDlgComponent-1.0.0.js", ResolveUrl("~/Shared/scripts/js/addNoteDlgComponent-1.0.0.js"));
var css = new HtmlLink();
css.Href = ResolveUrl("~/Shared/css/addNoteDlg-0.0.1.css");
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
Page.Controls.Add(css);
}
}
Instead, stuck the link tag in the user control markup, as there is no difference, practically speaking.
(The original motivation for doing it programmatically was so I could add it to the header, with Page.Header.Controls.Add(css), allowing me to keep all CSS links in the header.)
I want to show some .html files from an isolatedStorage, the files are located in
(MainFolder -> subFolder1 -> SubFolder2 -> page1.html).
How can I take that page1.html page from mainpage.xaml to secondpage.xaml and show on webControl browser?
In brief:
My secondPage.xaml contains a web Browser control so on that I want to
show that page1.html from mainpage.xaml, my page is located into above storage location.
So finally i have a proper answer for my question. my requirement was take values from isoalated Storage form Mainpage.xaml and show on SecondPage.xaml's web-browser control. And this all i wanted on selection a image from ListBox control which is set on the MainPage.xaml.
So code is following along with inline documentation i have mentioned to making out the scenario.
**//MainPage.xaml.**
private async void imgBookImage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//It is the way to take the id from the listView if you are showing your listView through Wrapper using ObservableCollection.
myWrapper bookName = (myWrapper)this.listCloudBooks.SelectedItem;
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
string dir = "/MainFolder/SubFolder/" + bookName.Id;
if (isf.DirectoryExists(dir))
{
NavigationService.Navigate(new Uri("/MainPage.xaml?selectedItem=" +bookName.Id, UriKind.Relative));//bookName.Id fetching Id of book and through bookid we will get the id on our destination page and will show the data.
}
else
{
//In this method i am creating folder of bookId and storing into the destination folder in my isolated storage. I am not showing the code of this method because i am answering of this question only.
DownloadBooks(bookName);
}
}
//SecondPage.xaml
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
string selectedBookId = "";//Set this variable globally.
position = 1;
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
string dir = "/MainFolder/SubFolder/" + selectedBookId;
string concatBookJson = string.Concat(dir, "/pageRecords.json");//Taking pageRecords.json, my this file have records of my *.html pages so it is showing from isolateStorage
if (isf.FileExists(concatBookJson))
{
CurrentBook = GetBookContents(concatBookJson);//Returning the Json Deserialize data from the isolated storage.
txtBookName.Text = CurrentBook.Title;//I have title property you can access all properties of your wrapper class (myWrapper).
}
}
You should use OnNavigated Event for fetching the values from MainPage.xaml, my requirement was different so i have shown in my PhoneApplciation_Loaded event.
I have 3 separate pages MainPage.xaml, Content.xaml and Nav.xaml. I need to load a Content.xaml in 'ucMainContent' userControl that sits inside MainPage.xaml by clicking a button from Nav.xaml. I am wondering what I am doing wrong. Any advice is highly appreciated.
MainPage.xaml where I set content container with this code:
<UserControl x:Name="ucMainContent" />
I am trying to load Content.xaml into mainPage.xaml from Nav.xaml. The code from Nav.xaml page where is the button:
private void LoadContent_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var contentPage = new Content();
var ucMainPage = new MainPage();
ucMainPage.ucMainContent.Content = contentPage;
}
If I understand you correctly you need to find the parent Control of Nav.xaml in this case Main.
Check out http://forums.silverlight.net/forums/t/55369.aspx - there is an excellent generic utility which allows you to find the parent of any usercontrol