I'm trying to write a silverlight application that takes in InitParams and then uses those InitParams to make a change to the Source of the MediaElement on the page. I'm trying to figure out the proper place to put my code.
I watched Tim Heuer's excellent video on InitParams, but in the video (which was for Silverlight 2), it shows the following on the Page.xaml.cs:
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
I don't see Page_Loaded when I open MainPage.xaml.cs, and I'm wondering if that was automatically created in the Silverlight 2 SDK and left out of the Silverlight 3 SDK. Or perhaps Tim added that in his video manually.
I find that I can go into the opening UserControl tag of MainPage.xaml and add Loaded="<New_Event_Handler>" which creates the following in MainPage.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
By default, there's also the following in MainPage.xaml.cs, which is run during the Application_Startup event in App.xaml.cs:
public MainPage()
{
InitializeComponent();
}
I need to figure out where is the best place to insert my code to change the Source on my MediaElement in my xaml. Should I put it in MainPage? Should I add the Loaded event handler and put it into UserControl_Loaded? If it's supposed to be Page_Loaded, where do I find that in Silverlight 3?
Any help would be much appreciated.
"UserControl_Loaded" and "Page_Loaded" are just method names and the names don't matter (you could name the method "Foo" if you wanted). What makes these methods do anything is the fact that they are attached to the Loaded event on the UserControl (which is what you did when you edited the MainPage.xaml file).
Related
i want to navigate txtBox1.text and bg2.source to another xaml at the same time
i try to use this
private void button1_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MultiGame), bg2.Source);
this.Frame.Navigate(typeof(MultiGame), txtBox1.text);
}
it doesnt work. I hope someone can help me!!!
Of course this does not work. As you call the Navigate method multiple times the Page MultiGameis loaded 2 times with each one of the parameters.
Why don`t you pack the two variables in one object array, and pass this to the navigate method?
Like
this.Frame.Navigate(typeof(MultiGame),new object[] {bg2.Source, txtBox1.text});
In the MultiGame class you now have to watch to the PageNavigated event. The navigation data is now your array.
I need to change the BackgroundImage of a button on click of another button (In Windows Forms in C#). But I can't find out how to do it!!
I searched on the internet and found many examples and all of them use ImageBrush, ImageSource etc.... but these don't work on my application, it shows me errors every time I Use them.
I read on the internet that I have to add this namespace:
using Windows.UI.Xaml.Media.Imaging;
But it shows me an error on the begging which says to add this System before Windwons and when I add it:
using System.Windows.UI.Xaml.Media.Imaging;
it shows me than the error at UI .... I can't figure it out how to solve this!!
Please help me guys!
To Change Background Image of a button there are two ways i know.
Add the Image to the resources folder of your project and use.
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.ImageName;
}
Use Image.FromFile();
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "//Card1.png");
}
You are trying to use solutions for WPF in Winforms. This will not work.
The class you need is System.Drawing.Image (or System.Drawing.Bitmap, which inherits from Image).
Bitmap b = new Bitmap(#"C:\myBitmap.jpg");
myButton.Image = b;
Be sure to call Dispose on your Bitmap if and when it is no longer in use.
I am trying to run a method called drawMoveLines() after my game page is fully loaded. This method, drawMoveLines(), draws lines between objects on my game page. However, when I try to run it at the bottom of my game page's main method, the lines go all screwy as the objects have not yet loaded correctly. (it works fine though when I run it off a button located in the game page)
I believe the path to go down involves asynchronous programming, but as my handle on this concept is rather primitive, any help in the right direction would be awesome.
Have found the correct syntax. It is as follows.
//This here
this.Loaded += new RoutedEventHandler(Page_Loaded);
//Calls this event
private void Page_Loaded(object sender, RoutedEventArgs e)
{
drawMoveLines();
}
My scenario is quite simple, in fact I thought it would cause no problem but it does, can anyone help?
My MainPage begins with this:
namespace PhoneApp
{
public partial class MainPage : PhoneApplicationPage { /* snip */ }
}
Later in the page is this simple method:
private void MakeSound(object sender, MouseButtonEventArgs e)
All right, everything is fine.
But on my SettingsSample.xaml I have a ListBoxItem that calls the method MakeSoundvia SelectionChanged property. VS studio tells me that 'event handler MakeSoundis not found on class PhoneApp.SettingsSample '
The code for this page begins with:
namespace PhoneApp
{
public partial class SettingsSample : PhoneApplicationPage { /* snip */ }
}
Copying the code of the method on SettingsPage.xaml.cs doesn't work for some reason, but I feel like it should possible to use the method described on MaingePage.xaml.cs, especially since their respective code resides in the same Namespace.
I tried to add
using PhoneApp;
on SettingsSample.xaml.cs. Not exceptionally clever, but I have no other idea
Your event function should read EventClickOnMyListBox and be located in the code-behind for your page. This function would then call your makeasound function (that you can put wherever you want). And it could be called by another event or function.
There is a semantics distinction between the event itself and what the event does. Your event is not makeasound, your event will cause a sound to be made, among possible other things.
If ever you want to add a popup to add a visual effect, you'll be stuck if you called directly the makeasound function, whereas you'll just have to add a line to your EventClickOnMyListBox in the other scenario.
That's why xaml works that way, and allows you to refer only to object in the enclosing class (or if there is a way I don't know it / and never needed it). You should minimize code-behind code (this is the way of thinking of the MVVM pattern if you want to go further down this road).
Replying to your comment, here is some code to get you started:
Your event should be in the code-behind of your SettingSample page, and should call the makeasound function instead of being the makeasound function:
private void MenuItem_Click(object sender, MouseButtonEventArgs e)
{
Whatever.Myclass.MakeASound (blabla); // So typically MakeASound would be static
}
And now same thing in your main page: you do not use your private void MakeSound(object sender, MouseButtonEventArgs e) as the event for a click, but an event handler just as the one above.
Take a step back and you'll see it's very natural: you want two things to do the same. But one thing cannot be the exact same thing as the first, for language-related reasons (read about C# and XAML to understand why).
So you put your feature in a third component, accessible from the first two. And you call this component from each of them. This way, they both share access to the same feature, and everybody is happy.
My Silverlight application has multiple XAML pages. For example, one displays a clock, one displays a timer. I have buttons to switch back and forth like so:
private void switchRight(object sender, RoutedEventArgs e)
{
this.Content = new Clock();
}
private void switchLeft(object sender, RoutedEventArgs e)
{
this.Content = new Timer();
}
I am trying to use the NavigationService to switch back and forth so I can have other pages running in the background rather than creating a new instance each time.
I am trying
NavigationService.Navigate(new uri("/Timer.xaml", UriKind.Relative));
but it doesn't seem to do anything and I can't find any good examples to help.
Here is a link
http://blogs.msdn.com/b/dphill/archive/2009/04/28/silverlight-navigation-part-3.aspx ,
Beside, I think you can use Threading for background processes.i.e.when you start a timer no need to show any xaml.
But for page instances you need to manage it very carefully otherwise stackoverflow :)
Depending on business rules its hard to decide navigation as being in a web browser.
We created our own Wizard (with rules). You may create your own NavigationManager. For validation I can offer http://fluentvalidation.codeplex.com/