如何要求将参数数组中的所有对象作为输出传递?在我的下面的代码中,我知道它失败了,因为我的原始参数永远不会被分配,因此是空的。虽然我认为他们会被作为参考而不必明确地说出来,但他们似乎并不是。结果,我不得不尝试将所有这些作为out参数传递。
这是我认为很重要的代码部分。
ChatController(删除了一些元素)
class ChatController : ControllerBase
{
#region Views
ChatAreaView viewChatArea;
UserListView viewUserArea;
MessageView viewMessageArea;
LoginPromptView viewLoginPrompt;
#endregion
#region ViewModels
ChatAreaViewModel viewModelChatArea;
UserAreaViewModel viewModelUserArea;
MessageAreaViewModel viewModelMessageArea;
LoginPromptViewModel viewModelLoginPrompt;
#endregion
public override void CreateViewsAndViewModels()
{
//InitializeViewAndViewModel(out viewChatArea, out viewModelChatArea);
//InitializeViewAndViewModel(out viewMessageArea, out viewModelMessageArea);
//InitializeViewAndViewModel(out viewUserArea, out viewModelUserArea);
//InitializeViewAndViewModel(out viewLoginPrompt, out viewModelLoginPrompt);
InitializeViewsAndViewModels(new FrameworkElement[] { viewChatArea, viewUserArea, viewMessageArea, viewLoginPrompt },
new object[] { viewModelChatArea, viewModelUserArea, viewModelMessageArea, viewModelLoginPrompt });
}
}
ControllerBase(删除了一些元素)
public abstract class ControllerBase : PrismBase
{
public void InitializeViewAndViewModel<TView, TViewModel>(out TView view, out TViewModel viewModel)
where TView : FrameworkElement, new()
where TViewModel : new()
{
view = new TView();
viewModel = new TViewModel();
view.DataContext = viewModel;
}
public void InitializeViewsAndViewModels(FrameworkElement[] views, object[] viewModels)
{
if (views.Length != viewModels.Length)
throw new ArgumentOutOfRangeException("views and viewModels must have the same number of elements.");
for (int i = 0; i < views.Length -1 ; i++)
InitializeViewAndViewModel(out views[i], out viewModels[i]);
}
}
问题区域在这里
public void InitializeViewsAndViewModels(FrameworkElement[] views, object[] viewModels)
{
}
当方法返回时,我传入方法的所有这些对象都保持为空。
InitializeViewsAndViewModels(new FrameworkElement[] { viewChatArea, viewUserArea, viewMessageArea, viewLoginPrompt },
new object[] { viewModelChatArea, viewModelUserArea, viewModelMessageArea, viewModelLoginPrompt });
我想要求将所有视图和 viewModels 数组对象作为输出,但我似乎只需要将整个数组本身作为输出传递。< / p>
我可以在这做什么?
编辑:如上所述,整个问题似乎源于该方法的复数形式。如果我摆脱了这一点,我只是这样做......
public override void CreateViewsAndViewModels()
{
InitializeViewAndViewModel(out viewChatArea, out viewModelChatArea);
InitializeViewAndViewModel(out viewMessageArea, out viewModelMessageArea);
InitializeViewAndViewModel(out viewUserArea, out viewModelUserArea);
InitializeViewAndViewModel(out viewLoginPrompt, out viewModelLoginPrompt);
//InitializeViewsAndViewModels(new FrameworkElement[] { viewChatArea, viewUserArea, viewMessageArea, viewLoginPrompt },
// new object[] { viewModelChatArea, viewModelUserArea, viewModelMessageArea, viewModelLoginPrompt });
}
一切都在游泳。
答案 0 :(得分:2)
您不能在C#中使用out
值数组。
现在你要做的事情非常奇怪,即使支持out
的数组,你仍然会以乱序结束 - 你在数组中丢失了变量类型,所以没有任何可靠的方法新的正确。
您当前的泛型方法在派生类中有多个实现 - 每对类型一个(即您的ChatController
具有您可以查看的4个不同的InitializeViewAndViewModel实现,每个实现特定的视图对)。你新提出的代码必须以某种方式找到丢失的类型信息......
答案 1 :(得分:1)
使用参考。见下文:
class ChatController : ControllerBase
{
#region Views
ChatAreaView viewChatArea;
//UserListView viewUserArea;
//MessageView viewMessageArea;
//LoginPromptView viewLoginPrompt;
#endregion
#region ViewModels
ChatAreaViewModel viewModelChatArea;
//UserAreaViewModel viewModelUserArea;
//MessageAreaViewModel viewModelMessageArea;
//LoginPromptViewModel viewModelLoginPrompt;
#endregion
public void CreateViewsAndViewModels()
{
//InitializeViewAndViewModel(out viewChatArea, out viewModelChatArea);
//InitializeViewAndViewModel(out viewMessageArea, out viewModelMessageArea);
//InitializeViewAndViewModel(out viewUserArea, out viewModelUserArea);
//InitializeViewAndViewModel(out viewLoginPrompt, out viewModelLoginPrompt);
var a = new FrameworkElement[] { viewChatArea };
var b = new object[] { viewModelChatArea};
InitializeViewsAndViewModels(a,
b);
}
}
public abstract class ControllerBase
{
public void InitializeViewAndViewModel<TView, TViewModel>(ref TView view, ref TViewModel viewModel)
where TView : FrameworkElement, new()
where TViewModel : new()
{
view = new TView();
viewModel = new TViewModel();
view.DataContext = viewModel;
}
public void InitializeViewsAndViewModels(FrameworkElement[] views, object[] viewModels)
{
if (views.Length != viewModels.Length)
throw new ArgumentOutOfRangeException("views and viewModels must have the same number of elements.");
for (int i = 0; i < views.Length; i++)
InitializeViewAndViewModel(ref views[i], ref viewModels[i]);
}
}