Bu soru 4 yıl önce istendi, ancak bu cevabı da öyle başkalarına yardım edebilir işte gidiyor -> Bunu basit ve hızlı bir şekilde yapmak için aşağı ve kirli, kod arkasında bir yöntemle çalıştırmak istediğiniz kodu koyun. daha sonra MainWindow() InitializeComponent()
'dan önce yöntemi çağırmanız yeterlidir. Bu tehlikeler ortaya çıkarır, ancak çoğu zaman çalışır çünkü bileşenler pencere başlatma/gösterilmeden önce yüklenir. (Bu projelerimin birinden kod çalışıyor.) Uygulama başlatıldığında kısa bir dalga dosyası oynatmak istediğinizi varsayalım. Böyle görünüyordu;
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();
}
}
}
Bu, arama yapanlara yardımcı olur. Proje inşa sonra :-)
Formdaki New() methid içinde kod çalışması yapmaz mısınız? –
[Buraya bakın] (http://msdn.microsoft.com/en-us/library/ms742302.aspx) – RajeshKdev