C# 逻辑运算符和条件运算符有问题
我很难使用逻辑/条件运算符找出正确的实现。我对一个应用程序有一个非常具体的要求,起初我认为它工作正常,但当我被放到市场上时,我发现我的实现根本不起作用C# 逻辑运算符和条件运算符有问题,c#,windows-phone-8,conditional-statements,logical-operators,C#,Windows Phone 8,Conditional Statements,Logical Operators,我很难使用逻辑/条件运算符找出正确的实现。我对一个应用程序有一个非常具体的要求,起初我认为它工作正常,但当我被放到市场上时,我发现我的实现根本不起作用 如果我的应用程序处于试用模式,并且保存的事件数>100,则我需要执行操作 如果应用程序处于试用模式,且保存的事件数为TrialExperienceHelper.Buy(), param=>TrialExperienceHelper.LicenseMode==TrialExperienceHelper.LicenseModes.Trial); }
#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.
OnSimulatedPurchase();
#else // DEBUG
// In release configuration, when the user returns to the application we will refresh the cache.
RefreshCache();
#endif // DEBUG
}
#endregion constructors
#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
#区域枚举
///
///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
}
#端域构造函数
#区域属性
///
///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();
}
///
///可以随时调用此方法来刷新缓存中存储的值。我们重新查询应用程序对象
///获取许可证的当前状态,并缓存新值。我们还提出了LicenseChanged事件。
///
公共静态空刷新缓存()
{
TrialExperienceHelper.isActiveCache=CurrentApp.LicenseInformation.IsActive;
TrialExperienceHelper.isTrialCache=CurrentApp.LicenseInformation.IsTrial;
TrialExperienceHelper.RaiseLicenseChanged();
}
私有静态void RaiseLicenseChanged()
{
如果(TrialExperienceHelper.LicenseChanged!=null)
{
TrialExperienceHelper.LicenseChanged();
}
}
#如果调试
私有静态void OnSimulatedPurchase()
{
TrialExperienceHelper.simulatedLicMode=LicenseModes.Full;
TrialExperienceHelper.RaiseLicenseChanged();
}
#endif//DEBUG
#端域法
#地区活动
///
///每当LicenseMode属性的值(可能)发生更改时,就会引发静态LicenseChanged事件。
///
公共静态事件许可证更改手持设备许可证更改;
#端区事件
这可能是一个暗箱操作,但在某些情况下(有时在Java中,但我对.NET不太确定)字符串与“==”的比较不起作用。您可以尝试使用“compare”方法而不是“==”您的条件运算符很好。问题可能来自于TrialViewModel.LicenseModustring的实际值。在发布的appOK中,我在我的应用程序中遵循了教程,而
string _license = TrialViewModel.LicenseModeString;
string _isTrial = "Trial";
// Use the overload of the Equals method that specifies a StringComparison.
// Ordinal is the fastest way to compare two strings.
bool result = _license.Equals(_isTrial, StringComparison.Ordinal);
Settings.SavedCount.Value += 1;
//both conditions must be true for the code to execute inside the 'if'
if (result && Settings.SavedCount.Value > 100)
{
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;
}
}
//either the entire first condition OR the second condition must be true to execute the method ApplyAndSaveAsync()
else if((result && Settings.SavedCount.Value <= 100) || TrialViewModel.LicenseModeString == "Full")
{
ApplyAndSaveAsync();
}
#region fields
private RelayCommand buyCommand;
#endregion fields
#region constructors
public TrialViewModel()
{
// Subscribe to the helper class's static LicenseChanged event so that we can re-query its LicenseMode property when it changes.
TrialExperienceHelper.LicenseChanged += TrialExperienceHelper_LicenseChanged;
}
#endregion constructors
#region properties
/// <summary>
/// You can bind the Command property of a Button to BuyCommand. When the Button is clicked, BuyCommand will be
/// invoked. The Button will be enabled as long as BuyCommand can execute.
/// </summary>
public RelayCommand BuyCommand
{
get
{
if (this.buyCommand == null)
{
// The RelayCommand is constructed with two parameters - the action to perform on invocation,
// and the condition under which the command can execute. It's important to call RaiseCanExecuteChanged
// on a command whenever its can-execute condition might have changed. Here, we do that in the TrialExperienceHelper_LicenseChanged
// event handler.
this.buyCommand = new RelayCommand(
param => TrialExperienceHelper.Buy(),
param => TrialExperienceHelper.LicenseMode == TrialExperienceHelper.LicenseModes.Trial);
}
return this.buyCommand;
}
}
public string LicenseModeString
{
get
{
return TrialExperienceHelper.LicenseMode.ToString()/* + ' ' + AppResources.ModeString*/;
}
}
#endregion properties
#region event handlers
// Handle TrialExperienceHelper's LicenseChanged event by raising property changed notifications on the
// properties and commands that
internal void TrialExperienceHelper_LicenseChanged()
{
this.RaisePropertyChanged("LicenseModeString");
this.BuyCommand.RaiseCanExecuteChanged();
}
#endregion event handlers
#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.
OnSimulatedPurchase();
#else // DEBUG
// In release configuration, when the user returns to the application we will refresh the cache.
RefreshCache();
#endif // DEBUG
}
#endregion constructors
#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