Silverlight helix code-behind page navigation - silverlight

I am new to silverlight and from what I gathered there isnt any direct functionality as regards to paging so I downloaded the helix project from here . I found it rather usefull but failed to find a way(using helix) to navigate the pages through code-behind . The reason why I need this is that I want to navigate to another page if a method executed successfully.

In the OnLoaded event of RootPage.xaml.cs you can see the following code:
this.rootFrame.Navigate( new Uri( "Page1.xaml", UriKind.Relative ) );
This programatically navigates to Page1.xaml (which implements NavigationPage) when the RootPage loads by calling the Navigate method of an instance of the Frame control defined in RootPage.xaml:
<h:Frame x:Name="rootFrame" Grid.Row="0" Grid.Column="1"
NavigationUIVisibility="Visible" Margin="4" />
This Navigate method in turn calls the Navigate method of the Frame's encapsulated StackJournal instance.
If you are in the code-behind of a page that does not have access to the parent Frame directly (i.e. any page other than RootPage) such as Page1.xaml you need to raise a RequestNavigate event that will bubble up to the nearest parent Frame.
The following code shows how to navigate programatically from a button click on Page1.xaml directly to Page3.xaml:
private void TestButton_Click(object sender, RoutedEventArgs e)
{
this.RaiseEvent(NavigationLink.RequestNavigateEvent,
new RequestNavigateEventArgs(new Uri("Page3.xaml", UriKind.Relative),
"rootFrame"));
}
Notice the targetName is "rootFrame", the parent Frame object that eventually performs the navigation.

Related

Wpf WebBrowser navigate throws COM exception

This is the code I was using, when I use webbrowser navigate to a page, it throws a COM exception. Is there any help here? Thanks.
Based on your comments in our conversation, your WebBrowser control is not loaded at the time you're trying to navigate. Therefore, you need to create a Loaded event handler and place your navigation code into that.
webBrowser.Loaded += WebBrowser_Loaded; // or in XAML
void WebBrowser_Loaded(object sender, RoutedEventArgs e)
{
// your navigation code here or set a flag
}
If you don't wish to go through the event route, you must, at the very least, check if the control IsLoaded prior to attempting navigation (in your particular scenario).
If the issue persists, create a new instance of the WebBrowser control and navigate through that.
webBrowser = new WebBrowser();

WPF Mainwindow closed automatically after refresh the child Usercontrol inside mainwindow

I am developing WPF application which contains MainWindow.xaml as container to display all user controls inside the Mainwindow .I have defined all the menus inside the Mainwindow.xaml and displaying the respective usercontol inside mainwindow.xaml grid.
Below is my Mainwindow grid definition and respective codebehind event handler code
<Grid x:Name="Container" Grid.Column="0" Width="0" Margin="0,0,0,10" Visibility="Visible">
</Grid>
Code Behind Menu click Event Handler
When the maindow is loaded after login it will call and display the CustomerListView user control
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CustomerListView CLView = new CustomerListView ();
Container.Children.Clear();
Container.Children.Add(CLView );
Container.Children[0].SetValue(WidthProperty, CLView.Width);
Container.Children[0].SetValue(HeightProperty, CLView.Height);
}
//Customer Menu Click Event
public void NewCustomer_Menu_Click(object sender, RoutedEventArgs e)
{
//AddCustomer is User control
AddCustomer AddEmployeeObject = new AddCustomer();
Container.Children.Clear();
Container.Children.Add(AddEmployeeObject);
Container.Children[0].SetValue(WidthProperty, AddEmployeeObject.Width);
Container.Children[0].SetValue(HeightProperty, AddEmployeeObject.Height);
}
Everything works fine until i integrate my application by new login control.After successful login i displayed mainwindow. If i am doing some actions like adding new customers(separate user control) or editing new customers(seperate user control) my application get closed automatically.How to overcome this issue?
Code to display mainwinow after logged in
if (IsValidUser)//Bool variable to validate user
{
//Hide the Loginwindow
this.Hide();
//Display mainwindow
MainWindow objMainWindow = new MainWindow();
objMainWindow.ShowDialog();
}
If i do any update or insert operations it closes the application after insert or update or refresh.
It seems every time Maindow is reloads after the insert or update or trying to load another user control or refresh the user control inside the mainwindow grid.How to resolve this?
You are clearing your Grid named Container every time you add a new Customer object by calling Container.Children.Clear(). Don't add new controls in code behind, instead define your UI in XAML and use data binding to visualize the objects you are adding.
Some links for you to read:
Data Binding Overview: http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx
ObservableCollection: http://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx
Woha !!! Got the solution finally. I have defined the button with IsCancel="True" property.So when ever i submit the data it closes the Mainwindow.It's just the copy paste mistake and finally removed the IsCancel attribute and works fine.

Hide navigation bar but remember the history in wpf frame

I have a frame on my main window. I have an ItemsControl in which I show the vertical menu from which user can select any Item and the page corresponding to specific Item is shown in the frame.
This works well.
When I set NavigationUIVisibility to Automatic:
1) Navigation Bar is displayed at the top.
2) If I enter some data in a textbox in page1 and then if I navigate away to page2.
Now again if I navigate to page1, the text in the textbox is there.
What I want is:
1) Hide the Navigation bar. For that I have set NavigationUIVisibility to Hidden.
This works good.
2) I want frame to remember history as I discussed in point 2 in above topic.
So, How to remember history when NavigationUIVisibility is set to hidden.
Or is there any other way to remember history when navigation bar is hidden.
You can re-style NavigationFrames and NavigationWindows so that they look completely different. In fact all of these images below are NavigationWindows, even the one without any navigation UI at all.
I put together an open source library for re-styling these at http://winchrome.codeplex.com so you can just steal the parts you need from the style. In fact if you just want a navigating panel with menu on the left, then its already covered in some of the demos for that project.
To answer your question as to why the TextBox clears it depends very much on what you are navigating to, and for some cases this is independent of the history.
Lets start simply, lets consider we have MyPage.xaml
<Page x:class="MyPage"...>
<TextBox/>
</Page>
If you are navigating to the same MyPage that you were before, then it will still have the same values in it.
If you are navigating to a new MyPage() then by default the TextBox will be empty.
If the navigation history is turned on, and you are navigating back, then you get the original MyPage and so see the same value in the TextBox.
Now lets consider if we are using MVVM style views bound to ViewModels.
In MyPage.xaml
<Page ...>
<TextBox Text="{Binding MyData}"/>
</Page>
In DataViewModel.cs
public class DataViewModel : INotifyPropertyChanged
{
private string myData;
public string MyData
{
get { return myData;}
set { ... }
//Normal implementation of INotifyPropertyChanged etc for MVVM
}
If you are navigating to a new Page() which is bound to the same ViewModel then both Pages will always be in sync and show the same data.
If navigating to the ViewModel and relying on DataTemplates to create a view, then you have still have two views but both sync from the same ViewModel.
If you use the history and navigate back, you get the original MyPage so see the original data again.
The Frame class has a property called BackStack which retains the back navigation history for a Frame. This property is available to you regardless of the NavigationUIVisibility setting.
Use PageFunction instead, which helps out you to navigate very backward retaining values for that pages..
Page1.xaml:
<PageFunction
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="XYZ"
x:TypeArguments="sys:String"
Title="page1">
<TextBox x:Name="textbox"/>
<Button x:Name="button" Click="button_clicked"/>
</PageFunction>
private void button_clicked(object sender, RoutedEventArgs e)
{
Page2 page2 = new Page2();
page2.Return += page2_Return;
this.NavigationService.Navigate(page2);
}
void page2_Return(object sender, ReturnEventArgs<String> e)
{
OnReturn(new ReturnEventArgs<String>(null));
}
Page2.Xaml:
<PageFunction
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="PQR"
x:TypeArguments="sys:String"
Title="page2">
...
</PageFunction>
void BackButton_Click(object sender, RoutedEventArgs e)
{
OnReturn(new ReturnEventArgs<String>(null));
}

Navigation in Silverlight 5

I want to navigate to new page on the click event of Next button. Suppose my current page is "FirstTestPage.xaml" and want to go on the page "SecondTestPage.xaml" on click on nextButton_Click Event
private void RadButton_Click(object sender, RoutedEventArgs e)
{
///CODE HERE
}
And also want the back button which transfer me to FirstTestPage.xaml.
Thank You
Please see my answer to the question: Making a Wizard in Silverlight + Xaml
to quote it: "I would suggest looking into the Silverlight Navigation Framework. It allows you to use "urls" to navigate between "pages" (which are your XAML user controls). It also also users to use the back and forth buttons in the browser, which may or may not be something you want to allow.
There is a VS 2010 template when you choose New Project, Silverlight, "Silverlight Navigation Application" that will help get you started."
You can also use a border or panel in the Xaml and change the content of that. Here's an example using border.
XAML
<Grid>
<Border x:Name="contentBorder" />
</Grid>
Code Behind:
private void RadButton_Click(object sender, RoutedEventArgs e)
{
this.contentBorder.Child = new SecondTestPage();
}
If you want to get fancy and have the Telerik controls (which I assume from your RadButton example) check out their transition control: http://demos.telerik.com/silverlight/#TransitionControl/FirstLook

using routed events within Silverlight user controls

within my current project file I have a user control that has a storyboard animation applied to the control. When a button is clicked in the page the storyboard starts and basically visually presents the control to the user. The storyboard resides in the current page as a resource
<navigation:Page.Resources>
<Storyboard x:Name="PreferncesOpen">....</Storyboard x:Name="PreferncesOpen">
</navigation:Page.Resources>
Within the page I have button that I have a click event on that starts the storyboard
private void btnOpenPreferences_Click(object sender, RoutedEventArgs e)
{
preferencesPanel.Visibility = System.Windows.Visibility.Visible;
PreferncesOpen.Begin();
}
Within the userControl (preferencesPanel) I have a button that when clicked needs to close/collapse the user control. I plan to do this using Visibility.collapsed. I assume that I need to use routed commands since the button is within the user control but the actions need to be called within the page that contains the control? I'm still new to routed commands and I assume this is the correct approach. I'm just unsure how to click on a button within the user control and have it modify or execute commands that would impact how the page (in which this control resides) may change or for that part affect other elements within the page? For example when the button is clicked within the user control I would like the visibility of the user control to be set to collapsed. I also would like to have the width of one of the grid columns within the main page re-size. I have done this in the past using the code behind for the page but I am trying to separate some of this and I thought routed commands would be the way to go?
I'd greatly appreciate any tips.
Thank you in advance
The title is a bit misleading, you're asking about commands rather then routed events if I understand you correctly.
Here's an example of using a DelegateCommand<T> from the Prism library; It happens to be my personal preference.
Markup :
<Button x:Name="MyButton" Content="Btn" Command="{Binding DoSomethingCommand}"/>
Code-behind* or ViewModel :
(* if you're not using MVVM make sure to add MyButton.DataContext = this; so you're sure that the button can databind to your code behind effectively)
public DelegateCommand<object> DoSomethingCommand
{
get
{
if(mDoSomethingCommand == null)
mDoSomethingCommand = new DelegateCommand(DoSomething, canDoSomething);
return mDoSomethingCommand;
}
private DelegateCommand<object> mDoSomethingCommand;
// here's where the command is actually executed
void DoSomething(object o)
{}
// here's where the check is made whether the command can actually be executed
// insert your own condition here
bool canDoSomething(object o)
{ return true; }
// here's how you can force the command to check whether it can be executed
// typically a reaction for a PropertyChanged event or whatever you like
DoSomethingCommand.RaiseCanExecuteChanged();
The argument that's passed to the above function is the CommandParameter dependency property (in Prism it's an attached property as well as the Command property if memory serves me right).
When it's set, you can pass a value of your choosing to the command that you wish to execute.
Hope that helps.

Resources