Autor Beitrag
marix
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Mo 08.11.10 16:15 
Hallo Forummitglieder,

Ich habe eine kleine eigene Messagebox erstellt, die als Alternative in einer Anwendung Warnmeldungen erzeugt.
Diese sollte als STAThread aufgerufen werden.
Es werden dabei 2 Strings übergeben.
Ich bin schon seit tagen am googeln und habe schon sämtliche Verweise auf ähnliche Probleme strikt durchgearbeitet aber ich kann den Fehler nicht beheben.

Hier der relevante Teil des Codes:


ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
namespace Mainproject.WPFMessageBoxControls
{
    internal delegate void Invoker();

    /// <summary>
    /// Interaction logic for MessageBox.xaml
    /// </summary>
    /// 
    public partial class WPFMessageBox : Window
    {
        #region Constants
        private const int YES_ID = 356;
        private const int NO_ID = 357;
        private const int OK_ID = 65;
        private const int CANCEL_ID = 64;
        public string strText;
        public string strCaption;
        public string strTextNew;
        public string strCaptionNew;
        public string[] strTC;
        public object data;


        #endregion

        #region variables
        //public delegate void ThreadProcDelegate(string strText, string strCaption);
        //public event ThreadProcDelegate MyMboxDlg;

        #endregion


        [ThreadStatic]
        public static WPFMessageBox _msgBox;
        public string strYes, strCancel, strOk, StrNo;


        public WPFMessageBox(string strText, string strCaption)
        {

            InitializeComponent();
            this.tblMessage.Text = strText;
            this.lblMsgWndHeader.Content = strCaption;
            this.InitializeMultiLangText();
            this.OnLanguageChanged();          

        }

        [STAThread]
        public static void ThreadProc(object data)
        {
            object[] ParamArray = (object[])data;
            string strText = (string)data[1];
            string strCaption = (string)data[2];
            WPFMessageBox WPFMBox = new WPFMessageBox(strText, strCaption);
            _msgBox.ShowDialog();
        }


        [STAThread]        
        public static bool? ShowMessage(string strText, string strCaption, WFPMessageBoxButtons msgBxBtn, MessageBoxImage image)

        {
            Thread newThreadProc = new Thread (new ParameterizedThreadStart(WPFMessageBox.ThreadProc));

            string strTextNew = strText;
            string strCaptionNew = strCaption;
            object[] data = new object[] { strTextNew, strCaptionNew };
            newThreadProc.SetApartmentState(ApartmentState.STA);
            newThreadProc.Name = "STAThread";
            newThreadProc.Start(_msgBox.data);
            

            if (System.Windows.Application.Current.Properties["MainWindow"is Window)
            {
                Window wndw = System.Windows.Application.Current.Properties["MainWindow"as Window;
                _msgBox.Owner = System.Windows.Application.Current.Properties["MainWindow"as Window;
            }

            UpdateUI(msgBxBtn);
            _msgBox.Close();
            return _msgBox.DialogResult;
        }

Die Fehlermeldung lautet:

Cannot apply indexing with [] to an expression of type 'object'

Kann mir jemand etwas Licht ins Dunkel bringen???

Herzlichen Dank

Gruss Markus ---marix---
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Mo 08.11.10 16:24 
Du versuchst ja auch per Index auf ein einzelnes Object(data) zuzugreifen anstatt auf das object[] (ParamArray ) das du erstellt bzw gecastet hast.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
 public static void ThreadProc(object data)
        {
            object[] ParamArray = (object[])data;
            string strText = (string)data[1];
            string strCaption = (string)data[2];
            WPFMessageBox WPFMBox = new WPFMessageBox(strText, strCaption);
            _msgBox.ShowDialog();
        }
marix Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Mo 08.11.10 17:19 
Hallo bakachan,

genau, ParamArray statt data... :idea:
vielen Dank, manchmal sieht man den Wald vor Bäumen nicht.


Leider habe ich jetzt bei:

newThreadProc.Start(_msgBox.data);

eine SystemNullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

evtl. noch eine Idee???


Viele Grüsse Markus
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Mo 08.11.10 17:25 
ähnliches Prinzip wie das letzte Problem:
ausblenden C#-Quelltext
1:
2:
WPFMessageBox WPFMBox = new WPFMessageBox(strText, strCaption);
            _msgBox.ShowDialog();

WPFMBox erstellt aber _msgBox(== null?) aufgerufen.
marix Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Mo 08.11.10 22:12 
Danke vielmals, ich komme aus der C++ Welt und habe einige Zeit nicht mehr programmiert, danke für die Hilfe.

Eine generelle Frage noch, wie kann ich solch einen Dialog generell von meiner MainGUI sepparieren, die selbstkreeirte MessageBox sollte generell unabhängig vom MainThread aufgerufen werden können, bin ich denn mit meinem Entwurf auf dem richtigen Weg?

Ich kann auch jeden Aufruf der MessageBox dispatchen, um Exceptions zu vermeiden, aber da es unheimlich viele Aufrufe sind, wäre eine zentrale Lösung viel besser.

Sorry für die banalen Fragen, wenn das zu sehr in die Grundlagen geht, schliesse ich diesen Thread als abgeschlossen ab.

Learning by Doing ist immer der beste Weg, aber manchmal brauch man einen Mentor der einem den Weg zeigt.


Viele Grüsse Markus