WPF如何将图像源从表单更改为另一个

时间:2016-01-19 01:59:20

标签: c# wpf

我有一个项目,我可以点击设置并打开设置表单,如果我根据彩色图像检查不同的单选按钮。图像将更改为显示特定颜色的另一个表单。我不断收到错误单击更改颜色按钮程序崩溃。我只是WPF的初学者,有谁知道我如何通过检查单选按钮并单击按钮来解决这个问题,其他表单的图像源也会相应更改?

设置表单

    private void btnchgcolour_Click(object sender, RoutedEventArgs e)
    {
        if(rdbred.IsChecked == true)
        {
            birdcolor = "redbird";
        }
        else if(rdbgreen.IsChecked == true)
        {
            birdcolor = "greenbird";
        }
        else if(rdbblack.IsChecked == true)
        {
            birdcolor = "blackbird";
        }
        this.DialogResult = true;


    }

    private void btnback_Click(object sender, RoutedEventArgs e)
    {
        MainWindow m1 = new MainWindow();
        m1.Show();
        this.Close();
    }
}

主页 public partial class MainWindow:Window     {         bool isPaused = false;         // bool isResume = false;

    private int time = 120;
    private DispatcherTimer Timer;
    private DispatcherTimer countdown;
    private int _angle;
    public int points {get; set;}
    bool boolClicked = false;

  //  public string selectBirdcolour { get; set; }






    public MainWindow()
    {
        InitializeComponent();
    }


    void Timer_tick(object sender, EventArgs e)
    {
        Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate
        {
           // points +=1;
            lblscoreamount.Content = points.ToString();

            _angle += 90;
            Birdie.RenderTransformOrigin = new Point(0.5, 0.5);
            Birdie.RenderTransform = new RotateTransform(-_angle);
            Birdie1.RenderTransformOrigin = new Point(0.5, 0.5);
            Birdie1.RenderTransform = new RotateTransform(-_angle);
            Birdie2.RenderTransformOrigin = new Point(0.5, 0.5);
            Birdie2.RenderTransform = new RotateTransform(-_angle);
            Birdie3.RenderTransformOrigin = new Point(0.5, 0.5);
            Birdie3.RenderTransform = new RotateTransform(-_angle);
            Birdie4.RenderTransformOrigin = new Point(0.5, 0.5);
            Birdie4.RenderTransform = new RotateTransform(-_angle);
            boolClicked = false;
            if (_angle == 360)
            {
                _angle = 0;
            }
        });

        Random r = new Random();
        int rand1 = r.Next(140, 480);
        int rand2 = r.Next(140, 620);




        Canvas.SetTop(Birdie, rand2);
        Canvas.SetLeft(Birdie, rand1 - 50);


        Canvas.SetTop(Birdie1, rand2);
        Canvas.SetLeft(Birdie1, rand1+20);


        Canvas.SetTop(Birdie2, rand2);
        Canvas.SetLeft(Birdie2, rand1 + 100);

        Canvas.SetTop(Birdie3, rand2);
        Canvas.SetLeft(Birdie3, rand1 - 110);

        Canvas.SetTop(Birdie4, rand2);
        Canvas.SetLeft(Birdie4, rand1 + 160);




        if (time > 0)
        {
            return;


            }

             else if(time == 0)
        {
           // time--;
            results sp1 = new results();
            this.Close();
            sp1.lblscoreresult.Content = lblscoreamount.Content;
            sp1.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            sp1.Show();
            Timer.Stop();


        }



        else if (time <= 5 )
            {
              //  Canvas.SetTop(Birdie, rand2);
              //  Canvas.SetLeft(Birdie, rand1 - 50);

              //  Canvas.SetTop(Birdie1, rand2);
              //  Canvas.SetLeft(Birdie1, rand1);

              //  Canvas.SetTop(Birdie2, rand2);
              //  Canvas.SetLeft(Birdie2, rand1 + 100);

              //  Canvas.SetTop(Birdie3, rand2);
              //  Canvas.SetLeft(Birdie3, rand1 - 100);

              //  Canvas.SetTop(Birdie4, rand2);
              //  Canvas.SetLeft(Birdie4, rand1 + 150);

               // boolClicked = false;
              //  MessageBox.Show("Hi");

              }



        else
        {

            Timer.Stop();
        }

        //if(isPaused)
        //{
        //    Timer.Stop();
        //}


    }

    private void Button_Click_Paused(object sender, RoutedEventArgs e)
    {
        if(isPaused != true)
        {
            isPaused = true;
            Timer.Stop();
            countdown.Stop();
    }
        else if(isPaused == true)
        {
            isPaused = false;
            Timer.Start();
            countdown.Start();
        }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {



    }


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Timer = new DispatcherTimer();
        Timer.Interval = new TimeSpan(0, 0, 5); //edit this part for the time interval
        Timer.Tick += Timer_tick;
        Timer.Start();

        countdown = new DispatcherTimer();
        countdown.Interval = new TimeSpan(0, 0, 1);
        countdown.Tick += Countdown_tick;
        countdown.Start();
        _angle=0;
        isPaused = false;











    }

    public void selectBirdcolour()
    {
        Settings set=new Settings();
        if(set.birdcolor == "redbird")
        {
            Birdie1.Source = new BitmapImage(new Uri("/inlandbirdred.png"));
        }
    }


    void Countdown_tick(object sender, EventArgs e)
    {
        if (time > 0)
        {
            time--;
            tbTime.Text = "00:" + (time / 60).ToString("d2") + ":" + (time % 60).ToString("d2");

        }
        else
        {

            Timer.Stop();
        }

    }

    private void btnup_Click(object sender, RoutedEventArgs e)
    {
       if (_angle == 180)
       {
           if (boolClicked == false)
           {
                   boolClicked = true;
                   points += 1;
                  //boolClicked = false;
           }
       }


       else
       {
           return;
       }



    }

    private void btndown_Click(object sender, RoutedEventArgs e)
    {
        if (_angle == 0)
        {
            if (boolClicked == false)
            {
                boolClicked = true;
                points += 1;
                //boolClicked = false;
            }
        }
        else
        {
            return;
        }

    }

    private void btnleft_Click(object sender, RoutedEventArgs e)
    {

        if (_angle == 270)
        {
            if (boolClicked == false)
            {
                boolClicked = true;
                points += 1;
                //boolClicked = false;
            }
        }
        else
        {
            return;
        }
    }

    private void btnright_Click(object sender, RoutedEventArgs e)
    {

        if (_angle == 90)
        {
            if (boolClicked == false)
            {
                boolClicked = true;
                points += 1;
                //boolClicked = false;
            }
        }
        else
        {
            return;
        }
    }

    private void btnsettings_Click(object sender, RoutedEventArgs e)
    {
        Settings setting = new Settings();

        setting.Show();
        this.Close();

    }





    }

1 个答案:

答案 0 :(得分:1)

我相信“设置”是您的设置形式?

您应该在主窗口中执行此操作:

public string BirdColor = "";

private void btnsettings_Click(object sender, RoutedEventArgs e)
{
        Settings setting = new Settings();
        setting.Show(this);
}

现在以您的设置形式

private void btnchgcolour_Click(object sender, RoutedEventArgs e)
{
    MainWindow m1 = (MainWindow)this.Parent;
    //if does not work use this.Owner;

    if(rdbred.IsChecked == true)
    {
        m1.BirdColor = "redbird";
    }
    else if(rdbgreen.IsChecked == true)
    {
        m1.BirdColor = "greenbird";
    }
    else if(rdbblack.IsChecked == true)
    {
        m1.BirdColor = "blackbird";
    }
}

private void btnback_Click(object sender, RoutedEventArgs e)
{
        MainWindow m1 = (MainWindow)this.Parent;
        //if does not work use this.Owner;
        m1.Show();
        this.Close();
}

然后BirdColor字符串应包含所选设置。

public void selectBirdcolour()
{
    if(BirdColor == "redbird")
    {
        Birdie1.Source = new BitmapImage(new Uri("/inlandbirdred.png"));
    }
}