0

アプリケーションでの 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();
        }

文字列比較でまたはのTrialViewModel.LicenseModeStringいずれかであることに注意してください。正しく機能しているようです。こちらも順調に増えています。ユーザーが試用版または製品版をダウンロードした場合、またはユーザーが試用版を製品版にアップグレードした場合に、アプリケーションで自動的に設定されるライセンス状態を照会するため、これは問題ではないと思います。何らかの理由で、Marketplace から試用版と完全版の両方の状態でアプリをテストすると、メソッドが実行されないのですか?TrialFullSettings.SavedCount.ValueTrialViewModel.LicenseModeStringApplyAndSaveAsync

のチェックを元に戻そうTrialViewModel.LicenseModeStringとしSettings.Saved.Count.Valueたので、カウントが最初にチェックされますが、これが役立つかどうかはわかりません。私はhttp://msdn.microsoft.com/en-us/library/aa691310(v=vs.71).aspxを参照しThe operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true. The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false.ました。

Settings.SavedCount.Value += 1;
        if ((Settings.SavedCount.Value > 100) && (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 <= 100) && (TrialViewModel.LicenseModeString == "Trial")) || (TrialViewModel.LicenseModeString == "Full"))
        {
            ApplySelectedEffectAndSaveAsync();
        }

最後の質問は、代わりに単一の&and|演算子を使用する必要があるかどうかです。

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();
        }

TrialViewModel

#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

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.

OnSimulatedPurchase(); #else // DEBUG // リリース構成では、ユーザーがアプリケーションに戻ったときにキャッシュを更新します。RefreshCache(); #endif // DEBUG } #endregion コンストラクター

    #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    
4

1 に答える 1

0

あなたのロジックは問題ないようです。正確にまたはをTrialViewModel.LicenseModeString返しますか?TrialFull

ちなみに、あなたの場合、&&or&演算子を使用しても違いはありません。論理式の一部がメソッドである場合、違いが生じます。例えば:

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 

最初のifステートメントは、最初に式の両側を評価し、式が真か偽かをチェックします。2 番目のステートメントは、評価を省略します。左式は false であるため、最終結果が false になることが既にわかっているため、右側は評価されません。

于 2013-10-24T05:54:18.637 に答える