यह सवाल 4 साल पहले कहा गया था, लेकिन इस सवाल का जवाब दूसरों भी तो मदद मिल सकती है यहां जाता है -> इसे आसानी से और तेज़ी से करने के लिए - नीचे और गंदे, उस कोड को रखें जिसे आप कोड-बैक में किसी विधि में चलाना चाहते हैं। तो बस MainWindow() InitializeComponent()
से पहले विधि को कॉल करें। यह खतरे पैदा करता है, लेकिन ज्यादातर बार यह काम करता है क्योंकि घटक खिड़की की शुरुआत/प्रदर्शन से पहले लोड होते हैं। (यह मेरी परियोजनाओं में से एक से कोड काम कर रहा है।) मान लीजिए कि ऐप आग लगने पर आप एक छोटी तरंग फ़ाइल खेलना चाहते हैं। यह इस तरह दिखेगा;
using ...
using System.Windows.Media;
namespace yourNamespace_Name
{
/// sumary >
/// Interaction logic for MainWindow.xaml
/// /sumary>
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
/*call your pre-written method w/ all the code you wish to
* run on project load. It is wise to set the method access
* modifier to 'private' so as to minimize security risks.*/
playTada();
InitializeComponent();
}
private void playTada()
{
var player = new System.Media.SoundPlayer();
player.Stream = Properties.Resources.tada;
// add the waveFile to resources, the easiest way is to copy the file to
// the desktop, resize the IDE window so the file is visible, right
// click the Project in the solution explorer & select properties, click
// the resources tab, & drag and drop the wave file into the resources
// window. Then just reference it in the method.
// for example: "player.Stream = Properties.Resources.tada;"
player.Play();
//add garbage collection before initialization of main window
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
उम्मीद है कि यह उन लोगों की सहायता करता है जो खोज रहे हैं। :-)
स्रोत
2014-03-01 10:00:33
क्या आप फॉर्म के नए() मेथिड में काम नहीं करेंगे? –
[यहां देखें] (http://msdn.microsoft.com/en-us/library/ms742302.aspx) – RajeshKdev