根据这个MSDN引用 IEnumerable是协变的,这可以隐式地将对象列表转换为可枚举:
IEnumerable<String> strings = new List<String>();
IEnumerable<Object> objects = strings;
Run Code Online (Sandbox Code Playgroud)
在我自己的代码中,我编写了一行代码,当列表的项类型为class时,它是完美的Point(Point是一个简单的类,有三个属性double x,y,z):
var objects = (IEnumerable<object>)dataModel.Value;
// here property Value is a list that could be of any type.
Run Code Online (Sandbox Code Playgroud)
但是当列表的项类型为double:时,上面的代码返回以下异常:
Unable to cast object of type System.Collections.Generic.List1[System.Double]
to type System.Collections.Generic.IEnumerable1[System.Object].
Run Code Online (Sandbox Code Playgroud)
是什么区别string和double和是什么原因导致代码的工作string,但不是double?
根据这篇文章,我们可以简单地将一个列表转换为IEnumerable(没有类型参数),因为我只需要迭代项目并将新项目添加到列表中(实际上我根本不需要列出列表中的项目).我决定用这个:
var objects = (IEnumerable)dataModel.Value;
Run Code Online (Sandbox Code Playgroud)
但是如果您需要将列表中的项目投射到object并使用它们,Theodoros的答案就是您最常遵循的解决方案.
我今天刚从5.4.1升级到5.5.0.但是当我构建我的android项目时,我在编译输出中得到了这些警告:
Warning: QML import could not be resolved in any of the import paths: QtQuick.Extras.Private
Warning: QML import could not be resolved in any of the import paths: QtQuick.Extras.Private.CppUtils
Warning: QML import could not be resolved in any of the import paths: QtQuick.Extras.Private.CppUtils
Warning: QML import could not be resolved in any of the import paths: HelperWidgets
Run Code Online (Sandbox Code Playgroud)
这是什么?我的项目在之前的版本中没有任何警告编译.
假设我们有一个像这样的通用View模型:
public class MyViewModel<T> : INotifyPropertyChanged where T : Class1
{
private T _objectModel;
public MyViewModel(T object)
{
_objectModel= object;
}
public event PropertyChangedEventHandler PropertyChanged;
}
Run Code Online (Sandbox Code Playgroud)
当我想将这个View Model绑定到DataContext我的UserControlin时XAML,我不能!XAML编辑器找不到My View Model类.我应该如何在XAML中引用泛型类型?
<UserControl.DataContext>
<s:MyViewModel<T>/> // How should I write this here????
</UserControl.DataContext>
Run Code Online (Sandbox Code Playgroud)
在上面的代码中s是我的工作区的别名,如果我将我的通用视图模型转换为具体类,它可以正常工作.
我有一个抽象类如下:
public abstract class Node
{
public abstract void run();
}
Run Code Online (Sandbox Code Playgroud)
有些子节点可能有几个属性作为输入和输出.但是所有操作都是在run()每个Node应该实现的方法中完成的.例如,绘制一条线的Node可能是这样的:
public class LineNode : Node
{
[Input]
public Point a;
[Input]
public Point b;
[Output]
public Line line;
public override void run()
{
line = new Line(a, b);
// draw line ...
}
}
[AttributeUsage(AttributeTargets.Field)]
public class Input : System.Attribute
{
}
[AttributeUsage(AttributeTargets.Field)]
public class Output : System.Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我没有关于子类中字段的任何先前信息.**为了使节点成为常规中的有效节点,只需要至少有一个输出.**我想强制所有其他用户在其节点中至少有一个输出,以便我可以通过反射来找到它寻找应用[Output]属性的字段.
这可能吗?
如果是这样,这是最好的方式吗?
谢谢你的帮助
这里提出了同样的问题,但建议的解决方案在运行时解决了它.我正在寻找面向对象的解决方案.哪一个更合乎逻辑?
我有这个主窗口:
ApplicationWindow {
id : mainWindow
width: 640
height: 480
visible: true
Button{
text: "go back to form 1"
onClicked: {
form2.visible = true;
}
}
SecondForm{
id: form2
}
}
Run Code Online (Sandbox Code Playgroud)
第二个窗口是:
Window{
id: main
width: 640
height: 480
x: 0
y: 0
visible: false;
Button{
text: "go back to form 1"
onClicked: {
main.visible = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
桌面版本是好的,但在Android运行应用程序时,它的行为很奇怪!当我单击mainWindow中的按钮时会发生此错误:W/Qt ( 8903): (null):0 ((null)): QEGLPlatformContext::swapBuffers(): eglError: 12301, this: 0x6b46e7c0虽然似乎调用了第二个窗体并且主窗口变为非活动状态.但第二个窗口不可见.虽然我看不到内部的按钮和按钮,当我触摸按钮预期位于的区域时,显然它可以工作,第二个窗口消失,然后第一个窗口再次激活.当我尝试通过单击android后退按钮返回mainWindow时,它会返回到mainWindow并发出此警告:W/Qt ( 8903): (null):0 ((null)): Can't find surface …
在我的代码中,我需要取消注册并注册一个事件处理程序,这非常有效:
_favoritsImageView.Click -= _favoritsImageView_Click(this, new CustomeClickEventArgs(item));
_favoritsImageView.Click += _favoritsImageView_Click(this, new CustomeClickEventArgs(item));
void _favoritsImageView_Click(object sender, CustomeClickEventArgs e)
{
// handles the event
}
Run Code Online (Sandbox Code Playgroud)
但对于一个等待的事件处理程序,我必须使用以下语法:
_favoritsImageView.Click -= async (s, e) =>
{ await _favoritsImageView_ClickAsync(s, new CustomeClickEventArgs(item)); };
_favoritsImageView.Click += async (s, e) =>
{ await _favoritsImageView_ClickAsync(s, new CustomeClickEventArgs(item)); };
async Task _favoritsImageView_ClickAsync(object sender, CustomeClickEventArgs e)
{
// does async task
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.因为匿名方法没有相同的引用.因此第一行不会取消注册已注册的处理程序.最后,第二行为点击添加了额外的事件处理程序.
我需要使用哪种语法来添加和删除异步事件处理程序?
谢谢你的任何建议.
我正在尝试使用数据绑定创建自定义视图.以下是自定义视图的代码:
package com.xxx.myapplication;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import com.xxx.myapplication.databinding.DataviewBinding;
/**
* Created by atp on 12/25/2016.
*/
public class DataView extends FrameLayout {
DataviewBinding binding;
public DataView(Context context) {
super(context);
}
public DataView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DataView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DataView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
binding = DataviewBinding.bind(this); …Run Code Online (Sandbox Code Playgroud) 我有这个字符串:
"round((TOTAL_QTY * 100) / SUM(ORDER_ITEMS->TOTAL_QTY) , 1)"
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码拆分字符串:
String[] tokens = function.split("[ )(*+-/^!@#%&]");
Run Code Online (Sandbox Code Playgroud)
结果是以下数组:
"round"
""
"TOTAL_QTY"
""
""
"100"
""
""
""
"SUM"
"ORDER_ITEMS"
"->TOTAL_QTY"
""
""
""
"1"
Run Code Online (Sandbox Code Playgroud)
但我需要按如下方式拆分字符串:
"round",
"TOTAL_QTY",
"100",
"SUM",
"ORDER_ITEMS->TOTAL_QTY",
"1"
Run Code Online (Sandbox Code Playgroud)
为了使它更清楚。首先,我需要->在拆分字符串时忽略,然后删除结果数组中的那些空字符串。
我想在以下代码中使用USB设备.它成功列出了usb设备并迭代它们.在下面的代码中,对象"device"是我需要打开的usbdevice.除了总是返回null值的OpenDevice()方法之外,一切似乎都很好!
[Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")]
[IntentFilter(new[] {UsbManager.ActionUsbDeviceAttached})]
[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]
public class MainActivity : Activity
{
int count = 1;
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
UsbManager manager = (UsbManager)GetSystemService(Context.UsbService);
UsbDevice device = null;
foreach (var dev in manager.DeviceList)
{
if (dev.Value.VendorId == 5401)
{
device = dev.Value;
}
}
var connection = manager.OpenDevice(device);
// Read some data! Most have just one port (port 0).
} …Run Code Online (Sandbox Code Playgroud) 当我们Loader在QML中更改组件的来源时,有没有办法应用动画?例如,假设我有以下内容Loader:
Loader {
anchors.fill: parent
id: loader
}
Run Code Online (Sandbox Code Playgroud)
我设置时,我想要一个从左到右的动画 loader.source = "content.qml"
谢谢你的帮助.