2015-12-18 62 views
0

我已經添加了一個DialogService以打開一個ProductView,到目前爲止ShowDetailDialog()按預期工作。如何解決不關閉.Close()?

問題:

我打電話Close()上的ProductView,認爲不會被關閉。我通過在調用對話框服務關閉方法時設置一個斷點來調試此問題。

當我介紹代碼時,null檢查顯示productView爲null,這可以防止調用Close()

有沒有人知道爲什麼productView爲null? (雖然它顯示在視圖上的數據)

DialogService:(承載顯示和關閉方法)

namespace MongoDBApp.Services 
{ 
    class DialogService : IDialogService 
    { 

     Window productView = null; 
     ProductView _productView; 

     public DialogService() 
     { 
      _productView = new ProductView(); 
     } 

     public void CloseDetailDialog() 
     { 

      if (productView != null) 
       productView.Close(); 
     } 

     public void ShowDetailDialog() 
     { 
      _productView.ShowDialog(); 
     } 
    } 
} 

ProductViewModel:(ProductVM的摘要,呼籲SaveCommand close方法)

 private void SaveProduct(object product) 
     { 
      _dialogService.CloseDetailDialog(); 
      Messenger.Default.Send<ProductModel>(SelectedProduct); 
     } 

CustomerOrdersViewmodel:(凡ShowDetailDialog()最初被稱爲)

 private void EditOrder(object obj) 
     { 
      Messenger.Default.Send<ProductModel>(SelectedProduct); 
      _dialogService.ShowDetailDialog();       
     } 
+0

你在哪裏調用'ShowDetailDialog'? –

+0

在CustomerOrdersVM中,我會立即發佈。 ShowDetailDialog正在工作,只是因爲某些原因沒有關閉。 –

+0

當使用ShowDialog時,你需要設置DialogResult屬性。 'Close'方法將用於非模態窗口。 –

回答

1

我通過在View上實現IDialogService來解決了這個問題。然後從ViewModel中調用Show()和Close()方法。

解決方案:

接口:

public interface IDialogService 
{ 
    void CloseDialog(); 
    void ShowDialog(EditProductViewModel prodVM); 

} 

檢視:

public partial class ProductView : Window, IDialogService 
{ 

    public ProductView() 
    { 
     InitializeComponent(); 
     this.DataContext = new EditProductViewModel(this); 

    } 

    public void CloseDialog() 
    { 
     if (this != null) 
      this.Visibility = Visibility.Collapsed; 
    } 

    public void ShowDialog(EditProductViewModel prodVM) 
    { 
     this.DataContext = prodVM; 
     this.Show(); 
    } 

    private void Window_Closed(object sender, EventArgs e) 
    { 
     this.Visibility = Visibility.Collapsed; 
    } 


} 

視圖模型#1:

private IDialogService _dialogService; 

    public CustomerOrdersViewModel(IDialogService dialogservice) 
    { 
     this._dialogService = dialogservice;     
    } 


    private void EditOrder(object obj) 
    { 
     EditProductViewModel pvm = new EditProductViewModel(_dialogService); 
     pvm.Present(pvm); 
     Messenger.Default.Send<ProductModel>(SelectedProduct);        
    } 

視圖模型#2:

private IDialogService _dialogService; 


    public EditProductViewModel(IDialogService dialogService) 
    { 
     this._dialogService = dialogService; 
    } 


    private void SaveProduct(object product) 
    { 
     SelectedProduct = SelectedProductTemp; 
     _dialogService.CloseDialog(); 
    } 

    public void Present(EditProductViewModel prodVM) 
    { 
     _dialogService.ShowDialog(prodVM); 
    } 
1

這就是我一直關閉窗戶的方法。

這裏將是我的命令:

class CancelCommand : ICommand 
    { 
     private NewTruckViewModel newTruck; 
     public CancelCommand(NewTruckViewModel vm) 
     { 
      newTruck = vm; 
     } 
     public event EventHandler CanExecuteChanged; 

     public bool CanExecute(object parameter) 
     { 
      return true; 
     } 

     public void Execute(object parameter) 
     { 
      newTruck.Cancel(); 
     } 
    } 

這裏是我的視圖模型,並且會從我的命令調用的方法:

private NewTruck myWnd; //View Declaration 

//Ctor where I set myView (myWnd) equal to a view that is passed in. 
public NewTruckViewModel(ObservableCollection<Truck> Trucks, NewTruck wnd, bool inEditTruck) 
     { 
      myEngine.stopHeartBeatTimer(); 
      editTruck = inEditTruck; 
      myWnd = wnd; 
      SaveTruckCommand = new SaveTruckCommand(this); 
      CancelCommand = new CancelCommand(this); 
      ClearCommand = new ClearCommand(this); 
      SetLevel1MTCommand = new SetLevel1MTCommand(this); 
      SetLevel2MTCommand = new SetLevel2MTCommand(this); 
      SetLevel3MTCommand = new SetLevel3MTCommand(this); 
      SetLevel1FLCommand = new SetLevel1FLCommand(this); 
      SetLevel2FLCommand = new SetLevel2FLCommand(this); 
      SetLevel3FLCommand = new SetLevel3FLCommand(this); 
      myTrucks = Trucks; 
     } 
    public void Cancel() 
      { 
       myWnd.Close(); 
      } 

這對我的作品。

相關問題