How to load content page into mainPage from another xaml page? - c#

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

Related

PopAsync not working when i use it in a TabbedPage ViewModels

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];
}

Unable to modify the control collection

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.)

Hide Nav Bar on Main Page - Xamarin Forms

I want to hide Nav bar on the main-start page of my app. but want to make it display with a title and back button on the second page. This is something I did, but it appears on the main page too.
In App.xaml.cs, I used this:
MainPage = new NavigationPage(new MainPage());
In corresponding login page, I created this:
private async void LoginPage_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new LoginPage());
}
When the user will click it'll take to the second page(login page).
Also, how can I change the color of the nav bar?
In the constructor of the page you want to hide the navigation bar, use the following code.
Xamarin.Forms.NavigationPage.SetHasNavigationBar(this, false);
To change the color of the NavigationBar, you can do this.
new NavigationPage(new MainPage())
{
BarBackgroundColor = Color.FromHex("#000000"),
BarTextColor = Color.White
};
As a bonus, you can also remove it via xaml:
<ContentPage ...namespaces... NavigationPage.HasNavigationBar="False">...
In App.cs use
MainPage = new YourProjectNamespace.MainPage();
This will hide your navigation bar on your first page

creating textbox when button click in WPF?

i already searched few stackoverflow threads related thisbut i have problem in code plz review my code and tell me where is the problem i am stuck and do not where is the problem i am new here please help me out
public partial class MainWindow : Window
{
Grid MainGrid = new Grid();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "Type Partnumber";
Grid.SetRow(dynamicTextBox, 1);
Grid.SetColumn(dynamicTextBox, 0);
this.MainGrid.Children.Add(dynamicTextBox);
}
}
By using the button_Click event you are adding the dynamic TextBox to the MainGrid and that code looks fine. But the problem is that this.MainGrid is not in the Present UI, its similar to the dynamically created TextBox, since you ware defined it in the code-behind(see the definition above the constructor Grid MainGrid = new Grid();).
Consider that canContainer is a canvas defined in the xaml as like the following,
<Canvas Height="319" Margin="0"
Width="517"
Name="canContainer"/>
To overcome this you can choose any of the following method.
1.Add The MainGrid to the UI. it will add the grid to the canvas, keep in mind the dynamic textBox already added to the Grid.
which means the code should be like this -->
this.canContainer.Children.Add(MainGrid);
2.Add dynamicTextBox to any other parent which is present in the UI.
this.canContainer.Children.Add(dynamicTextBox);
You you are using this method then need not to define and adding MainGrid

How to bind Windows Form Control to a "Grid" Panel using MVVM in WPF

I am attempting to use MVVM to Bind a Windows Form Control to a panel in WPF. My overall objective is to be able to dynamically change which specific Windows Form Control I will use as I plan on having potentially several available.
Right now, I have been able to get this to work by having the application launch a callback on initialization which accesses the grid object by name. Here is how XAML currently looks:
<Grid Name="WindowsControlObject"></Grid>
The Callback looks like the following:
private void WindowLoaded(object sender, RoutedEventArgs e)
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Control activeXControl = new SomeWindowsControl();
host.Child = activeXControl;
this.WindowsControlObject.Children.Add(host);
}
While this works, I am trying to fully utilize the MVVM pattern, as such is there a way I can do something like the following in the XAML/ModelView:
XAML:
<Grid Content="{Binding WindowsControl"></Grid>
In my ModelView:
public class MyModelView
{
public Grid WindowsControl;
public MyModelView{
WindowsControl = new Grid;
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Control activeXControl = new SomeWindowsControl();
host.Child = activeXControl;
WindowsControl.WindowsControlObject.Children.Add(host);
}
}
Am I even right in my exploration/possible approach? It has occurred to me that I might need to use some other type of panel (other than grid), but haven't found anything obvious yet. If it can't be done, I have a solution, just not a very clean one.
Doing more digging, it turns out that I really wanted to bind this to a "ContentControl" tag, as follows:
XAML:
<ContentControl Content="{Binding WindowsControl}"/>
ViewModel:
private System.Windows.Forms.Control _myControl;
public WindowsFormsHost STKObject
{
get
{
return new WindowsFormsHost() { Child = _myControl};
}
}

Categories