C# 条件运算符和逻辑运算符求值的区别

C# 条件运算符和逻辑运算符求值的区别,c#,windows-phone-7,windows-phone-8,operators,logic,C#,Windows Phone 7,Windows Phone 8,Operators,Logic,我很难在应用程序中正确实现if-else语句。目前,我正在使用条件运算符&&和|进行比较,但当我的应用程序在市场上启动时,我的代码没有按计划运行。基本上,它的工作方式是,如果计数大于100且应用程序仍然是试用许可证,则要求升级;否则,如果计数小于或等于100且应用程序仍然是试用许可证或应用程序是完全许可证,则允许执行ApplyAndSaveAsync方法 Settings.SavedCount.Value += 1; if ((Settings.SavedCount.Value

我很难在应用程序中正确实现if-else语句。目前,我正在使用条件运算符
&&
|
进行比较,但当我的应用程序在市场上启动时,我的代码没有按计划运行。基本上,它的工作方式是,如果计数大于100且应用程序仍然是试用许可证,则要求升级;否则,如果计数小于或等于100且应用程序仍然是试用许可证或应用程序是完全许可证,则允许执行ApplyAndSaveAsync方法

Settings.SavedCount.Value += 1;
        if ((Settings.SavedCount.Value > 100) && (TrialViewModel.LicenseModeString == "Trial"))
        {
            MessageBoxResult result = MessageBox.Show("You have saved over 100 items! Would you like to continue?", "Congratulations", MessageBoxButton.OKCancel);
            switch (result)
            {
                case MessageBoxResult.OK:
                    // A command takes a parameter and in this case we can pass null.
                    TrialViewModel.BuyCommand.Execute(null);
                    break;
                case MessageBoxResult.Cancel:
                    return;
                    break;
            }
        }
        else if (((Settings.SavedCount.Value <= 100) && (TrialViewModel.LicenseModeString == "Trial")) || (TrialViewModel.LicenseModeString == "Full"))
        {
            ApplyAndSaveAsync();
        }
我的最后一个问题是,我应该使用一个
&
|
操作符吗

Settings.SavedCount.Value += 1;
        if ((Settings.SavedCount.Value > 1) & (TrialViewModel.LicenseModeString == "Trial"))
        {
            //MessageBoxResult result = MessageBox.Show("You have saved over 100 filtered pictures! Would you like to continue?", "Congratulations", MessageBoxButton.OKCancel);
            MessageBoxResult result = MessageBox.Show(AppResources.EditPage_MessageBoxContent_Purchase, AppResources.EditPage_MessageBoxCaption_Purchase, MessageBoxButton.OKCancel);
            switch (result)
            {
                case MessageBoxResult.OK:
                    // A command takes a parameter and in this case we can pass null.
                    TrialViewModel.BuyCommand.Execute(null);
                    break;
                case MessageBoxResult.Cancel:
                    if (editPagePivotControl != null && editPagePivotControl.SelectedIndex != 0)
                    {
                        //trialPopup.IsOpen = false;
                        editPagePivotControl.SelectedIndex = 0;
                    }
                    break;
            }
        }
        else if (((Settings.SavedCount.Value <= 1) & (TrialViewModel.LicenseModeString == "Trial")) | (TrialViewModel.LicenseModeString == "Full"))
        {
            ApplySelectedEffectAndSaveAsync();
        }
Settings.SavedCount.Value+=1;
如果((Settings.SavedCount.Value>1)和(TrialViewModel.LicenseModeString==“试用版”))
{
//MessageBoxResult=MessageBox.Show(“您已经保存了100多张过滤过的图片!是否要继续?”,“恭喜”,MessageBoxButton.OKCancel);
MessageBoxResult=MessageBox.Show(AppResources.EditPage\u MessageBoxContent\u Purchase,AppResources.EditPage\u MessageBoxCaption\u Purchase,MessageBoxButton.ok取消);
开关(结果)
{
case MessageBoxResult.OK:
//命令接受一个参数,在这种情况下,我们可以传递null。
TrialViewModel.BuyCommand.Execute(null);
打破
案例MessageBoxResult。取消:
如果(editPagePivotControl!=null&&editPagePivotControl.SelectedIndex!=0)
{
//trialPopup.IsOpen=假;
editPagePivotControl.SelectedIndex=0;
}
打破
}
}
else if(((Settings.SavedCount.Value TrialExperienceHelper.Buy()),
param=>TrialExperienceHelper.LicenseMode==TrialExperienceHelper.LicenseModes.Trial);
}
返回此.buy命令;
}
}
公共字符串许可证
{
得到
{
返回TrialExperienceHelper.LicenseMode.ToString()/*+''+AppResources.ModeString*/;
}
}
#端域属性
#区域事件处理程序
//通过在上引发属性更改通知来处理TrialExperienceHelper的LicenseChanged事件
//属性和命令
内部无效TrialExperienceHelper_LicenseChanged()
{
本.RaiseProperty变更(“许可证转让”);
this.BuyCommand.raisecancecutechanged();
}
#endregion事件处理程序
TrialExperienceHelper.cs

#region enums
    /// <summary>
    /// The LicenseModes enumeration describes the mode of a license.
    /// </summary>
    public enum LicenseModes
    {
        Full,
        MissingOrRevoked,
        Trial
    }
    #endregion enums

    #region fields
#if DEBUG
    // Determines how a debug build behaves on launch. This field is set to LicenseModes.Full after simulating a purchase.
    // Calling the Buy method (or navigating away from the app and back) will simulate a purchase.
    internal static LicenseModes simulatedLicMode = LicenseModes.Trial;
#endif // DEBUG
    private static bool isActiveCache;
    private static bool isTrialCache;
    #endregion fields

    #region constructors
    // The static constructor effectively initializes the cache of the state of the license when the app is launched. It also attaches
    // a handler so that we can refresh the cache whenever the license has (potentially) changed.
    static TrialExperienceHelper()
    {
        TrialExperienceHelper.RefreshCache();
        PhoneApplicationService.Current.Activated += (object sender, ActivatedEventArgs e) => TrialExperienceHelper.
#if DEBUG
            // In debug configuration, when the user returns to the application we will simulate a purchase.
#区域枚举
/// 
///LicenseModes枚举描述许可证的模式。
/// 
公共枚举许可证模式
{
满满的,
失踪或失踪,
试验
}
#端域枚举
#区域字段
#如果调试
//确定调试生成在启动时的行为。此字段在模拟购买后设置为LicenseModes.Full。
//调用Buy方法(或离开应用程序返回)将模拟购买。
内部静态LicenseModes simulatedLicMode=LicenseModes.试用版;
#endif//DEBUG
私有静态bool-isActiveCache;
私有静态布尔-伊斯特里奇;
#端域字段
#区域构造函数
//静态构造函数在应用程序启动时有效地初始化许可证状态的缓存。它还附加
//处理程序,以便在许可证(可能)更改时刷新缓存。
静态TrialExperienceHelper()
{
TrialExperienceHelper.RefreshCache();
PhoneApplicationService.Current.Activated+=(对象发送者,ActivateDevenTargets e)=>TrialExperienceHelper。
#如果调试
//在调试配置中,当用户返回应用程序时,我们将模拟购买。
OnSimulatedPurchase(); #else//DEBUG //在版本配置中,当用户返回应用程序时,我们将刷新缓存。 刷新缓存(); #endif//DEBUG } #端域构造函数

    #region properties
    /// <summary>
    /// The LicenseMode property combines the active and trial states of the license into a single
    /// enumerated value. In debug configuration, the simulated value is returned. In release configuration,
    /// if the license is active then it is either trial or full. If the license is not active then
    /// it is either missing or revoked.
    /// </summary>
    public static LicenseModes LicenseMode
    {
        get
        {
#if DEBUG
            return simulatedLicMode;
#else // DEBUG
            if (TrialExperienceHelper.isActiveCache)
            {
                return TrialExperienceHelper.isTrialCache ? LicenseModes.Trial : LicenseModes.Full;
            }
            else // License is inactive.
            {
                return LicenseModes.MissingOrRevoked;
            }
#endif // DEBUG
        }
    }

    /// <summary>
    /// The IsFull property provides a convenient way of checking whether the license is full or not.
    /// </summary>
    public static bool IsFull
    {
        get
        {
            return (TrialExperienceHelper.LicenseMode == LicenseModes.Full);
        }
    }
    #endregion properties

    #region methods
    /// <summary>
    /// The Buy method can be called when the license state is trial. the user is given the opportunity
    /// to buy the app after which, in all configurations, the Activated event is raised, which we handle.
    /// </summary>
    public static void Buy()
    {
        MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
        marketplaceDetailTask.ContentType = MarketplaceContentType.Applications;
        marketplaceDetailTask.Show();
    }

    /// <summary>
    /// This method can be called at any time to refresh the values stored in the cache. We re-query the application object
    /// for the current state of the license and cache the fresh values. We also raise the LicenseChanged event.
    /// </summary>
    public static void RefreshCache()
    {
        TrialExperienceHelper.isActiveCache = CurrentApp.LicenseInformation.IsActive;
        TrialExperienceHelper.isTrialCache = CurrentApp.LicenseInformation.IsTrial;
        TrialExperienceHelper.RaiseLicenseChanged();
    }

    private static void RaiseLicenseChanged()
    {
        if (TrialExperienceHelper.LicenseChanged != null)
        {
            TrialExperienceHelper.LicenseChanged();
        }
    }

#if DEBUG
    private static void OnSimulatedPurchase()
    {
        TrialExperienceHelper.simulatedLicMode = LicenseModes.Full;
        TrialExperienceHelper.RaiseLicenseChanged();
    }
#endif // DEBUG
    #endregion methods

    #region events
    /// <summary>
    /// The static LicenseChanged event is raised whenever the value of the LicenseMode property has (potentially) changed.
    /// </summary>
    public static event LicenseChangedEventHandler LicenseChanged;
    #endregion events    
#区域属性
/// 
///LicenseMode属性将许可证的活动状态和试用状态合并为一个
///枚举值。在调试配置中,返回模拟值。在发布配置中,
///如果许可证处于活动状态,则为试用版或完全版。如果许可证未处于活动状态,则
///它要么丢失,要么被撤销。
/// 
公共静态许可模式许可模式
{
得到
{
#如果调试
返回模拟模式;
#else//DEBUG
if(TrialExperienceHelper.isActiveCache)
{
return TrialExperienceHelper.isTrialCache?LicenseModes.试用版:LicenseModes.Full;
}
else//许可证处于非活动状态。
{
返回LicenseModes.MissingOrRevoked;
}
#endif//DEBUG
}
}
/// 
///IsFull属性提供了检查许可证是否已满的便捷方法。
/// 
公共静态布尔已满
{
得到
{
返回(TrialExperienceHelper.LicenseMode==LicenseModes.Full);
}
}
#端域属性
#区域方法
/// 
///当许可证状态为试用时,可以调用Buy方法。用户将获得此机会
///购买应用程序之后,在所有配置中,激活事件都会引发,我们会处理该事件。
/// 
公共静态无效购买()
{
MarketplaceDetailTask MarketplaceDetailTask=新的MarketplaceDetailTask();
marketplaceDetailTask.ContentType=MarketplaceContentType.Applications;
marketplaceDetailTask.Show();
}
/// 
///可以随时调用此方法来刷新值
#region enums
    /// <summary>
    /// The LicenseModes enumeration describes the mode of a license.
    /// </summary>
    public enum LicenseModes
    {
        Full,
        MissingOrRevoked,
        Trial
    }
    #endregion enums

    #region fields
#if DEBUG
    // Determines how a debug build behaves on launch. This field is set to LicenseModes.Full after simulating a purchase.
    // Calling the Buy method (or navigating away from the app and back) will simulate a purchase.
    internal static LicenseModes simulatedLicMode = LicenseModes.Trial;
#endif // DEBUG
    private static bool isActiveCache;
    private static bool isTrialCache;
    #endregion fields

    #region constructors
    // The static constructor effectively initializes the cache of the state of the license when the app is launched. It also attaches
    // a handler so that we can refresh the cache whenever the license has (potentially) changed.
    static TrialExperienceHelper()
    {
        TrialExperienceHelper.RefreshCache();
        PhoneApplicationService.Current.Activated += (object sender, ActivatedEventArgs e) => TrialExperienceHelper.
#if DEBUG
            // In debug configuration, when the user returns to the application we will simulate a purchase.
    #region properties
    /// <summary>
    /// The LicenseMode property combines the active and trial states of the license into a single
    /// enumerated value. In debug configuration, the simulated value is returned. In release configuration,
    /// if the license is active then it is either trial or full. If the license is not active then
    /// it is either missing or revoked.
    /// </summary>
    public static LicenseModes LicenseMode
    {
        get
        {
#if DEBUG
            return simulatedLicMode;
#else // DEBUG
            if (TrialExperienceHelper.isActiveCache)
            {
                return TrialExperienceHelper.isTrialCache ? LicenseModes.Trial : LicenseModes.Full;
            }
            else // License is inactive.
            {
                return LicenseModes.MissingOrRevoked;
            }
#endif // DEBUG
        }
    }

    /// <summary>
    /// The IsFull property provides a convenient way of checking whether the license is full or not.
    /// </summary>
    public static bool IsFull
    {
        get
        {
            return (TrialExperienceHelper.LicenseMode == LicenseModes.Full);
        }
    }
    #endregion properties

    #region methods
    /// <summary>
    /// The Buy method can be called when the license state is trial. the user is given the opportunity
    /// to buy the app after which, in all configurations, the Activated event is raised, which we handle.
    /// </summary>
    public static void Buy()
    {
        MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
        marketplaceDetailTask.ContentType = MarketplaceContentType.Applications;
        marketplaceDetailTask.Show();
    }

    /// <summary>
    /// This method can be called at any time to refresh the values stored in the cache. We re-query the application object
    /// for the current state of the license and cache the fresh values. We also raise the LicenseChanged event.
    /// </summary>
    public static void RefreshCache()
    {
        TrialExperienceHelper.isActiveCache = CurrentApp.LicenseInformation.IsActive;
        TrialExperienceHelper.isTrialCache = CurrentApp.LicenseInformation.IsTrial;
        TrialExperienceHelper.RaiseLicenseChanged();
    }

    private static void RaiseLicenseChanged()
    {
        if (TrialExperienceHelper.LicenseChanged != null)
        {
            TrialExperienceHelper.LicenseChanged();
        }
    }

#if DEBUG
    private static void OnSimulatedPurchase()
    {
        TrialExperienceHelper.simulatedLicMode = LicenseModes.Full;
        TrialExperienceHelper.RaiseLicenseChanged();
    }
#endif // DEBUG
    #endregion methods

    #region events
    /// <summary>
    /// The static LicenseChanged event is raised whenever the value of the LicenseMode property has (potentially) changed.
    /// </summary>
    public static event LicenseChangedEventHandler LicenseChanged;
    #endregion events    
int i = 0;
if ((i >= 100) & (a.IncreaseCounter() > 5))
  // a.IncreaseCounter() will be executed even while i is less than 100

if ((i >= 100) && (a.IncreaseCounter() > 5))
  // a.IncreaseCounter() will not be excuted