Frage

habe ich ein sehr interessantes Problem. Ich benutze diese Technologien in WPF-Anwendung. Caliburn.Micro und MEF

ich öffnen neue Fenster (nicht-Bildschirm) aus Sicht Modell. Es funktioniert gut.

In Init-View-Modell Ich habe diese Methode, die sie öffnen neue WPF-Fenster, nicht Bildschirm in der Schale.

...

        public IEnumerable<IResult> Send()
        {
            yield return new ShowWindow("NewScreen")
                .InitializeWith(_service.DetailData(Account,_selectedFriend.Key));
        }
...

Showwindow Klasse sieht wie folgt aus:

public class ShowWindow : IResult
{
    readonly Type _windowType;
    readonly string _name;

    [Import]
    public IShellViewModel Shell { get; set; }

    Action<object> _initializationAction = window => { };

    public ShowWindow InitializeWith<T>(T argument)
    {
        _initializationAction = window =>
        {
            var initializable = window as IInitializable<T>;
            if (initializable != null)
                initializable.Initialize(argument);
        };
        return this;
    }

    public ShowWindow(string name)
    {
        _name = name;
    }

    public ShowWindow(Type windowType)
    {
        _windowType = windowType;
    }

    public void Execute(ActionExecutionContext context)
    {
        var window = !string.IsNullOrEmpty(_name)
            ? IoC.Get<object>(_name)
            : IoC.GetInstance(_windowType, null);

        _initializationAction(window);

        IoC.Get<IWindowManager>().Show(window);

        Completed(this, new ResultCompletionEventArgs());
    }

    public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };

    public static ShowWindow Of<T>()
    {
        return new ShowWindow(typeof(T));
    }
}

Ich verwende Event Aggregator auf Nachrichten von init View-Modell zu einem neuen Fenster zu senden.

Alles funktionierte gut, bis ich richtebox Kontrolle hinzugefügt. Ich brauche bindable richtextbox. So I

verwenden bindable Version von Jason Mueller (http://social.msdn.microsoft.com/forums/en-US/wpf/thread/f77c011a-0aba-449f-b6f4-920e58ebf997/)

New-View-Modell sieht wie folgt aus:

public class NewViewModel : Screen, IInitializable<DetailData>, IHandle<string>
{
    private IEventAggregator _eventAgg;

    private FlowDocument _conversation;

    //bind on document of richtextBox
    public FlowDocument Conversation
    {
        get { return _conversation; }
        set
        {
            _conversation = value;
            NotifyOfPropertyChange("Conversation");
        }
    }

    [ImportingConstructor]
    public NewViewModel(IEventAggregator eventAgg)
    {
        _eventAgg = eventAgg;
        _eventAgg.Subscribe(this);

        **//I think problem is here
        _conversation = new FlowDocument();**
    }

    public void Handle(string message)
    {
        Conversation.Blocks
            .Add(new Paragraph(new Run(message)));
    }
}

In New-View-Modell-Klasse I bindet Eigenschaft Gespräch auf RichTextBox in Aussicht.

:

   <Controls:BindableRichTextBox    Document="{Binding Path=Conversation, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                                    VerticalScrollBarVisibility="Auto" 
                                    HorizontalScrollBarVisibility="Auto"
                                    FontSize="13"
                                    Margin="4,4,4,4" 
                                    Grid.Row="0" />

Das Problem ist.

  1. Ich nenne Methode public IEnumerable Send () von Init-View-Modell -> es genannt cotructor von New-View-Modell -> und es eröffnete neue Fenster. Das ist richtig

  2. Als ich Aufruf Methode public IEnumerable Send () zweites Mal und ich bekomme diese Fehlermeldung: System.Argument.Exception { "Dokument gehört bereits zu einem anderen RichTextBox"}

Dieser Fehler erhalte ich in der Klasse von bindable RichTextbox.

...

        protected override void OnInitialized(EventArgs e)
        {
            // Hook up to get notified when DocumentProperty changes.
            DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(DocumentProperty, typeof(BindableRichTextBox));
            descriptor.AddValueChanged(this, delegate
            {
                // If the underlying value of the dependency property changes,
                // update the underlying document, also.
**line 54:        base.Document = (FlowDocument)GetValue(DocumentProperty);**

            });

            // By default, we support updates to the source when focus is lost (or, if the LostFocus
            // trigger is specified explicity.  We don't support the PropertyChanged trigger right now.
            this.LostFocus += new RoutedEventHandler(BindableRichTextBox_LostFocus);

            base.OnInitialized(e);

        }

....

Ich denke Problem ist, weil es nur einmal Konstruktor von New-View-Modell nennen. So nenne ich fünfmal Methode senden, aber es nennt Konstruktor von New-View-Modell nur einmal. Wie kann es lösen?

Stacktrace:

   at System.Windows.Controls.RichTextBox.set_Document(FlowDocument value)
   at Spirit.Controls.BindableRichTextBox.b__0(Object , EventArgs ) in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_v1.2\Controls\BindableRichTextBox.cs:line 54
   at MS.Internal.ComponentModel.PropertyChangeTracker.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
   at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp)
   at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.Activate(Object item)
   at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
   at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Run(Object arg)
   at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

Wo ist es Problem, versuche ich viele Möglichkeiten, aber jede nicht funktioniert. Vielen Dank für Ihre Beratung und Hilfe. Ich bin hilflos.

War es hilfreich?

Lösung

Meine Lösung:

ich an meine neue Sicht Modell mit [PartCreationPolicy (CreationPolicy.NonShared)] , da die Standard-Lebensdauer von MEF Exporte Geteilt (Singletons).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top