按钮单击一次

时间:2016-06-11 17:30:12

标签: c# wpf button

我在打开时选择按钮的选项有问题,我做错了什么?

using UnityEngine;
using System.Collections;
public class pickup : MonoBehaviour {
public Transform OnHand;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    if (Input.GetButtonDown ("E")) {
        GetComponent<Rigidbody>().useGravity=false;
        this.transform.position = OnHand.position;
        this.transform.parent = GameObject.Find ("FPSController").transform;
        this.transform.parent = GameObject.Find ("FirstPersonCharacter").transform;

    }           
    if (Input.GetMouseButtonDown (0)) {
        this.transform.parent = null;
        GetComponent<Rigidbody>().useGravity=false;
//      Rigidbody.AddForce (new Vector2(1,4), ForceMode.Impulse);
    }   
}
}

2 个答案:

答案 0 :(得分:3)

我会避免保留自己的标志变量并手动管理其设置。例如,如果用户关闭窗口,您如何更改回变量以允许再次打开SettingsWindow?。在这个问题上有一种更强大的基于系统的方法。查看系统提供的信息将帮助您避免再次打开设置窗口,直到已经打开一个实例。

要检查是否已打开SettingsWindow的实例,您可以使用系统提供的信息,使用此类代码

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Check if, in the Application.Current.Windows collection 
    // there is at least one window of type SettingsWindow
    SettingsWindow w = Application.Current.Windows
                                  .OfType<SettingsWindow>()
                                  .FirstOrDefault();

    if(w == null)
    {
        // No window of the type required, open a new one....
        w = new SettingsWindow();
        w.Owner = System.Windows.Application.Current.MainWindow;
        w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        w.Top = this.Top + 20;
    }

    // Show it NON MODALLY....
    w.Show();
}

Show的调用立即返回(非模态),因此您的程序将照常继续,MainWindow仍处于活动状态。
相反,如果你想使用模态方法,(意味着在SettingsWindow打开之前,你的MainWindow中没有任何东西是活动的)你可以简单地创建SettingsWindow,设置它的所有者并最终设置它的位置,最后调用{{1 (不要忘记设置Owner属性)。这样,您的代码在ShowDialog中被阻止,并且在用户关闭刚刚打开的SettingsWindow实例之前不会返回。 (你可以删除上面的所有检查)

答案 1 :(得分:0)

很确定这将永远是真的 选择Steve的优秀答案

buttonWasClicked = true;
if (buttonWasClicked == true)
{
   // this will execute every time
}
else 
{
   // this will never execute
}
相关问题